2024-03-15 20:07:20 -04:00
|
|
|
import { Controller } from "@hotwired/stimulus"
|
2024-03-21 18:24:47 -04:00
|
|
|
import L, { circleMarker } from "leaflet"
|
2024-05-25 16:14:55 -04:00
|
|
|
import "leaflet.heat"
|
2024-03-15 20:07:20 -04:00
|
|
|
|
|
|
|
|
// Connects to data-controller="maps"
|
|
|
|
|
export default class extends Controller {
|
|
|
|
|
static targets = ["container"]
|
|
|
|
|
|
|
|
|
|
connect() {
|
2024-03-16 16:31:07 -04:00
|
|
|
console.log("Map controller connected")
|
2024-03-15 20:07:20 -04:00
|
|
|
var markers = JSON.parse(this.element.dataset.coordinates)
|
2024-04-04 11:31:15 -04:00
|
|
|
var center = markers[markers.length - 1] || JSON.parse(this.element.dataset.center)
|
2024-03-16 16:31:07 -04:00
|
|
|
var center = (center === undefined) ? [52.516667, 13.383333] : center;
|
2024-04-17 15:54:04 -04:00
|
|
|
|
|
|
|
|
var map = L.map(this.containerTarget, {
|
2024-04-17 16:00:23 -04:00
|
|
|
layers: [this.osmMapLayer(), this.osmHotMapLayer()]
|
2024-04-17 15:54:04 -04:00
|
|
|
}).setView([center[0], center[1]], 14);
|
|
|
|
|
|
2024-05-29 17:00:35 -04:00
|
|
|
var markersArray = this.markersArray(markers);
|
|
|
|
|
var markersLayer = L.layerGroup(markersArray);
|
|
|
|
|
var heatmapMarkers = markers.map(element => [element[0], element[1], 0.3]); // lat, lon, intensity
|
2024-04-17 15:54:04 -04:00
|
|
|
|
2024-05-29 17:00:35 -04:00
|
|
|
// Function to calculate distance between two lat-lng points using Haversine formula
|
|
|
|
|
function haversineDistance(lat1, lon1, lat2, lon2) {
|
|
|
|
|
const toRad = x => x * Math.PI / 180;
|
|
|
|
|
const R = 6371; // Radius of the Earth in kilometers
|
|
|
|
|
const dLat = toRad(lat2 - lat1);
|
|
|
|
|
const dLon = toRad(lon2 - lon1);
|
|
|
|
|
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
|
|
|
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
|
|
|
|
|
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
|
|
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
|
|
return R * c * 1000; // Distance in meters
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var splitPolylines = [];
|
|
|
|
|
var currentPolyline = [];
|
|
|
|
|
|
|
|
|
|
// Process markers and split polylines based on the distance
|
|
|
|
|
for (let i = 0, len = markers.length; i < len; i++) {
|
|
|
|
|
if (currentPolyline.length === 0) {
|
|
|
|
|
currentPolyline.push(markers[i].slice(0, 2));
|
|
|
|
|
} else {
|
|
|
|
|
var lastPoint = currentPolyline[currentPolyline.length - 1];
|
|
|
|
|
var currentPoint = markers[i].slice(0, 2);
|
|
|
|
|
var distance = haversineDistance(lastPoint[0], lastPoint[1], currentPoint[0], currentPoint[1]);
|
|
|
|
|
|
|
|
|
|
if (distance > 500) {
|
|
|
|
|
splitPolylines.push([...currentPolyline]); // Use spread operator to clone the array
|
|
|
|
|
currentPolyline = [currentPoint];
|
|
|
|
|
} else {
|
|
|
|
|
currentPolyline.push(currentPoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Add the last polyline if it exists
|
|
|
|
|
if (currentPolyline.length > 0) {
|
|
|
|
|
splitPolylines.push(currentPolyline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Batch adding polylines to the map
|
|
|
|
|
var polylineLayers = splitPolylines.map(polylineCoordinates =>
|
|
|
|
|
L.polyline(polylineCoordinates, { color: 'blue', opacity: 0.6, weight: 3 })
|
|
|
|
|
);
|
|
|
|
|
var polylinesLayer = L.layerGroup(polylineLayers).addTo(map);
|
|
|
|
|
|
|
|
|
|
var heatmapLayer = L.heatLayer(heatmapMarkers, { radius: 20 }).addTo(map);
|
2024-04-17 15:54:04 -04:00
|
|
|
|
|
|
|
|
var controlsLayer = {
|
|
|
|
|
"Points": markersLayer,
|
2024-05-29 17:00:35 -04:00
|
|
|
"Polylines": L.layerGroup(polylinesLayer),
|
2024-05-25 16:14:55 -04:00
|
|
|
"Heatmap": heatmapLayer
|
2024-05-29 17:00:35 -04:00
|
|
|
};
|
2024-03-15 20:07:20 -04:00
|
|
|
|
2024-05-29 17:00:35 -04:00
|
|
|
L.control.layers(this.baseMaps(), controlsLayer).addTo(map);
|
2024-05-25 16:14:55 -04:00
|
|
|
|
2024-03-21 18:54:19 -04:00
|
|
|
this.addTileLayer(map);
|
2024-04-21 11:15:52 -04:00
|
|
|
// markersLayer.addTo(map);
|
2024-03-21 18:54:19 -04:00
|
|
|
this.addLastMarker(map, markers);
|
2024-03-15 20:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
|
this.map.remove();
|
|
|
|
|
}
|
2024-03-21 18:24:47 -04:00
|
|
|
|
2024-04-17 16:00:23 -04:00
|
|
|
osmMapLayer() {
|
|
|
|
|
return L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
|
|
|
maxZoom: 19,
|
|
|
|
|
attribution: '© OpenStreetMap'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osmHotMapLayer() {
|
|
|
|
|
return L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
|
|
|
|
|
maxZoom: 19,
|
|
|
|
|
attribution: '© OpenStreetMap contributors, Tiles style by Humanitarian OpenStreetMap Team hosted by OpenStreetMap France'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baseMaps() {
|
|
|
|
|
return {
|
|
|
|
|
"OpenStreetMap": this.osmMapLayer(),
|
|
|
|
|
"OpenStreetMap.HOT": this.osmHotMapLayer()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
controlsLayer() {
|
|
|
|
|
return {
|
|
|
|
|
"Points": this.markersLayer,
|
|
|
|
|
"Polyline": this.polylineLayer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markersArray(markers_data) {
|
|
|
|
|
var markersArray = []
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < markers_data.length; i++) {
|
|
|
|
|
var lat = markers_data[i][0];
|
|
|
|
|
var lon = markers_data[i][1];
|
|
|
|
|
|
|
|
|
|
var popupContent = this.popupContent(markers_data[i]);
|
|
|
|
|
var circleMarker = L.circleMarker([lat, lon], {radius: 4})
|
|
|
|
|
|
|
|
|
|
markersArray.push(circleMarker.bindPopup(popupContent).openPopup())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return markersArray
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 18:24:47 -04:00
|
|
|
popupContent(marker) {
|
|
|
|
|
return `
|
|
|
|
|
<b>Timestamp:</b> ${this.formatDate(marker[4])}<br>
|
|
|
|
|
<b>Latitude:</b> ${marker[0]}<br>
|
|
|
|
|
<b>Longitude:</b> ${marker[1]}<br>
|
2024-03-22 17:57:53 -04:00
|
|
|
<b>Altitude:</b> ${marker[3]}m<br>
|
|
|
|
|
<b>Velocity:</b> ${marker[5]}km/h<br>
|
2024-03-28 10:11:59 -04:00
|
|
|
<b>Battery:</b> ${marker[2]}%
|
2024-03-21 18:24:47 -04:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatDate(timestamp) {
|
|
|
|
|
let date = new Date(timestamp * 1000); // Multiply by 1000 because JavaScript works with milliseconds
|
|
|
|
|
|
2024-05-25 14:23:33 -04:00
|
|
|
let timezone = this.element.dataset.timezone;
|
|
|
|
|
|
|
|
|
|
return date.toLocaleString('en-GB', { timeZone: timezone });
|
2024-03-21 18:24:47 -04:00
|
|
|
}
|
2024-03-21 18:54:19 -04:00
|
|
|
|
|
|
|
|
addTileLayer(map) {
|
|
|
|
|
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
|
|
|
maxZoom: 19,
|
|
|
|
|
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
|
|
|
|
|
}).addTo(map);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 11:54:01 -04:00
|
|
|
addMarkers(map, markers_data) {
|
|
|
|
|
var markers = []
|
|
|
|
|
for (var i = 0; i < markers_data.length; i++) {
|
|
|
|
|
var lat = markers_data[i][0];
|
|
|
|
|
var lon = markers_data[i][1];
|
2024-03-21 18:54:19 -04:00
|
|
|
|
2024-03-23 11:54:01 -04:00
|
|
|
var popupContent = this.popupContent(markers_data[i]);
|
2024-03-21 18:54:19 -04:00
|
|
|
var circleMarker = L.circleMarker([lat, lon], {radius: 4})
|
|
|
|
|
|
2024-03-23 11:54:01 -04:00
|
|
|
markers.push(circleMarker.bindPopup(popupContent).openPopup())
|
2024-03-21 18:54:19 -04:00
|
|
|
}
|
2024-03-23 11:54:01 -04:00
|
|
|
|
|
|
|
|
L.layerGroup(markers).addTo(map);
|
2024-03-21 18:54:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addPolyline(map, markers) {
|
|
|
|
|
var coordinates = markers.map(element => element.slice(0, 2));
|
|
|
|
|
L.polyline(coordinates).addTo(map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addLastMarker(map, markers) {
|
|
|
|
|
if (markers.length > 0) {
|
|
|
|
|
var lastMarker = markers[markers.length - 1].slice(0, 2)
|
|
|
|
|
L.marker(lastMarker).addTo(map);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-15 20:07:20 -04:00
|
|
|
}
|