realtime updates of markers

This commit is contained in:
NatureFreshMilk 2019-02-08 14:35:29 +01:00
parent 757d460379
commit 97d32a5210
2 changed files with 19 additions and 4 deletions

View File

@ -38,7 +38,7 @@ api.getConfig().then(function(cfg){
overlays["Lua Controller"] = new LuacontrollerOverlay(wsChannel, layerMgr);
overlays["Technic Anchor"] = new TechnicAnchorOverlay(wsChannel, layerMgr);
overlays["Technic Quarry"] = new TechnicQuarryOverlay(wsChannel, layerMgr);
//overlays["Protector"] = new ProtectorOverlay(wsChannel, layerMgr);
overlays["Protector"] = new ProtectorOverlay(wsChannel, layerMgr);
//Default enabled overlays
map.addLayer(overlays["Player"]);

View File

@ -12,9 +12,21 @@ var AbstractIconOverlay = L.LayerGroup.extend({
this.currentObjects = {};
this.onLayerChange = this.onLayerChange.bind(this);
this.onMapObjectUpdated = this.onMapObjectUpdated.bind(this);
this.onMapMove = debounce(this.onMapMove.bind(this), 50);
},
//websocket update
onMapObjectUpdated: function(obj){
var hash = self.hashPos(obj.x, obj.y, obj.z);
var marker = self.currentObjects[hash];
if (marker) {
//marker exists
marker.setPopupContent(self.createPopup(obj));
}
},
hashPos: function(x,y,z){
return x + "/" + y + "/" + z;
},
@ -65,14 +77,15 @@ var AbstractIconOverlay = L.LayerGroup.extend({
objects.forEach(function(obj){
var hash = self.hashPos(obj.x, obj.y, obj.z);
var marker = self.currentObjects[hash];
if (self.currentObjects[hash]) {
if (marker) {
//marker exists
//TODO: update popup
marker.setPopupContent(self.createPopup(obj));
} else {
//marker does not exist
var marker = L.marker([obj.z, obj.x], {icon: self.getIcon(obj)});
marker = L.marker([obj.z, obj.x], {icon: self.getIcon(obj)});
marker.bindPopup(self.createPopup(obj));
marker.addTo(self);
@ -88,6 +101,7 @@ var AbstractIconOverlay = L.LayerGroup.extend({
map.on("zoomend", this.onMapMove);
map.on("moveend", this.onMapMove);
this.layerMgr.addListener(this.onLayerChange);
this.wsChannel.addListener("mapobject-created", this.onMapObjectUpdated);
this.reDraw(true)
},
@ -96,6 +110,7 @@ var AbstractIconOverlay = L.LayerGroup.extend({
map.off("zoomend", this.onMapMove);
map.off("moveend", this.onMapMove);
this.layerMgr.removeListener(this.onLayerChange);
this.wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated);
}
});