Я строю систему слежения, она получает сигнал местоположения от websocket
и отображает местоположение с иконкой на карте.
Мне нужно сделать кластеризацию, я прочитал это но не понял, как изменить мой код для работы с ним.
Код 'openlayers':
<script>
var content = document.getElementById('popup-content');
var center = ol.proj.transform([44.6753, 25.7136], 'EPSG:4326', 'EPSG:3857'); //initial position of map
// ol.proj.fromLonLat([44.6753, 25.7136])
var view = new ol.View({
center: center,
zoom: 6
}); // {"unique_id": "Riyadh", "lat": 24.7136, "lon": 46.6753, "speed": 1}
//raster layer on map
var OSMBaseLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
straitSource = new ol.source.Vector({ wrapX: true });
var straitsLayer = new ol.layer.Vector({
source: straitSource
});
map = new ol.Map({
layers: [OSMBaseLayer, straitsLayer],
target: 'map',
view: view,
controls: [new ol.control.FullScreen(), new ol.control.Zoom()]
});
var icon = new ol.style.Icon({
anchor: [0.5, 0.5], // Default value is the icon center.
scale: 0.3,
color: '#ffcd46',
crossOrigin: 'anonymous',
src: 'http://127.0.0.1:8081/static/img/truck128.png'
});
var iconStyle = new ol.style.Style({
image: icon
});
// Popup showing the position the user clicked
var container = document.getElementById('popup');
var popup = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
map.addOverlay(popup);
/* Add a pointermove handler to the map to render the popup.*/
map.on('pointermove', function (evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel, function (feat, layer) {
return feat;
});
if (feature && feature.get('type') == 'Point') {
var coordinate = evt.coordinate; //default projection is EPSG:3857 you may want to use ol.proj.transform
content.innerHTML = feature.get('desc');
popup.setPosition(coordinate);
} else {
popup.setPosition(undefined);
}
});
</script>
И код websocket
:
var socket = new WebSocket("ws://127.0.0.1:8080/ws");
console.log(window.location.host)
var devices = []; // new Array();
var markers = {}; // new Object();
socket.onopen = function (event) {}
socket.onoclose = function (event) {}
socket.onerror = function (error) {
console.log('Error ${error.message}')
}
socket.onmessage = function (event) {
var messages = document.getElementById("messages");
var obj = JSON.parse(event.data);
var device = obj.unique_id;
var data=[{"Lon":19.455128,"Lat":41.310575}];
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([obj.lon, obj.lat], 'EPSG:4326', 'EPSG:3857')),
type: 'Point',
desc: ' First Location ' + '
' + 'Latitude : ' + obj.lon + '
Longitude: ' + obj.lat + '
'});iconFeature.setStyle (iconStyle);if (! devices.includes (device)) {devices.push (device);координаты var = [obj.lon, obj.lat];маркеры [устройство] = iconFeature;straitSource.addFeature (iconFeature);} else {straitSource.removeFeature (маркеры [устройство]);straitSource.addFeature (iconFeature);маркеры [устройство] = iconFeature;}};