1
0
forked from MTSR/mapserver
mapserver/server/static/js/overlays/AbstractIconOverlay.js

131 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-02-05 11:09:35 +03:00
'use strict';
var AbstractIconOverlay = L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr, type, icon) {
L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.type = type;
this.icon = icon;
this.currentObjects = {};
this.onLayerChange = this.onLayerChange.bind(this);
2019-02-08 16:35:29 +03:00
this.onMapObjectUpdated = this.onMapObjectUpdated.bind(this);
2019-02-05 11:09:35 +03:00
this.onMapMove = debounce(this.onMapMove.bind(this), 50);
},
2019-02-08 16:35:29 +03:00
//websocket update
onMapObjectUpdated: function(obj){
2019-02-08 19:41:09 +03:00
var hash = this.hashPos(obj.x, obj.y, obj.z);
var marker = this.currentObjects[hash];
2019-02-08 16:35:29 +03:00
if (marker) {
//marker exists
2019-02-08 19:41:09 +03:00
var popup = this.createPopup(obj);
2019-02-08 17:12:40 +03:00
if (popup)
marker.setPopupContent(popup);
marker.setIcon(this.getIcon(obj));
2019-02-08 16:35:29 +03:00
}
},
2019-02-05 11:09:35 +03:00
hashPos: function(x,y,z){
2019-02-05 23:17:14 +03:00
return x + "/" + y + "/" + z;
2019-02-05 11:09:35 +03:00
},
onLayerChange: function(layer){
this.reDraw(true);
},
onMapMove: function(){
this.reDraw(false);
},
2019-02-08 15:53:16 +03:00
getIcon: function(obj){
return this.icon;
},
2019-02-05 11:09:35 +03:00
reDraw: function(full){
var self = this;
2019-02-08 16:48:06 +03:00
if (this.map.getZoom() < 10) {
2019-02-05 11:09:35 +03:00
this.clearLayers();
this.currentObjects = {};
return;
}
if (full){
this.clearLayers();
this.currentObjects = {};
}
var mapLayer = this.layerMgr.getCurrentLayer()
2019-02-08 16:48:06 +03:00
var min = this.map.getBounds().getSouthWest();
var max = this.map.getBounds().getNorthEast();
2019-02-05 11:09:35 +03:00
var y1 = parseInt(mapLayer.from/16);
var y2 = parseInt(mapLayer.to/16);
var x1 = parseInt(min.lng);
var x2 = parseInt(max.lng);
var z1 = parseInt(min.lat);
var z2 = parseInt(max.lat);
api.getMapObjects(
x1, y1, y1,
x2, y2, z2,
this.type)
.then(function(objects){
2019-02-05 14:54:42 +03:00
//TODO: remove non-existing markers
2019-02-05 11:09:35 +03:00
objects.forEach(function(obj){
var hash = self.hashPos(obj.x, obj.y, obj.z);
2019-02-08 16:35:29 +03:00
var marker = self.currentObjects[hash];
2019-02-05 14:54:42 +03:00
2019-02-08 16:35:29 +03:00
if (marker) {
2019-02-05 14:54:42 +03:00
//marker exists
2019-02-08 17:12:40 +03:00
//set popup, if changed
var popup = self.createPopup(obj);
if (popup)
marker.setPopupContent(popup);
//redraw icon, if changed
marker.setIcon(self.getIcon(obj));
2019-02-05 14:54:42 +03:00
} else {
//marker does not exist
2019-02-08 16:35:29 +03:00
marker = L.marker([obj.z, obj.x], {icon: self.getIcon(obj)});
2019-02-08 17:12:40 +03:00
var popup = self.createPopup(obj);
if (popup)
marker.bindPopup(popup);
2019-02-05 14:54:42 +03:00
marker.addTo(self);
self.currentObjects[hash] = marker;
}
2019-02-05 11:09:35 +03:00
});
})
},
onAdd: function(map) {
2019-02-08 16:48:06 +03:00
this.map = map;
2019-02-05 11:09:35 +03:00
map.on("zoomend", this.onMapMove);
map.on("moveend", this.onMapMove);
this.layerMgr.addListener(this.onLayerChange);
2019-02-08 16:35:29 +03:00
this.wsChannel.addListener("mapobject-created", this.onMapObjectUpdated);
2019-02-05 11:09:35 +03:00
this.reDraw(true)
},
onRemove: function(map) {
this.clearLayers();
map.off("zoomend", this.onMapMove);
map.off("moveend", this.onMapMove);
this.layerMgr.removeListener(this.onLayerChange);
2019-02-08 16:35:29 +03:00
this.wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated);
2019-02-05 11:09:35 +03:00
}
});