Skip to content

Commit

Permalink
Added ability to get tile layer and check if a tile layer exists
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouthgate committed Oct 14, 2020
1 parent 216b23c commit 2f68e9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ L.MultiMap.Map = L.Class.extend({
// tileLayerIds [{string}] The IDs of the tile layers that should use this map
// zoomOffset {number} The difference in zoom for this map from the multimap zoom.
// vanillaOptions {object} Options which should be passed to the L.Map instance
// vanillaMap {L.Map} The vanilla leaflet map instance
// Private Properties:
// _overlayMaps {<Name> -> L.LayerGroup} Overlays which have been added to this map
// _tileLayerTitles [{string}] The names of the tile layers that should use this map
// _vanillaMap {L.Map} The vanilla leaflet map instance
// _vanillaLayersControl {L.Control.Layers} The layer controls used on this map
// _vanillaTileLayers {<Name> -> L.TileLayer} A map from a tile layer's name to the vanilla leaflet tile layer

Expand All @@ -16,17 +16,17 @@ L.MultiMap.Map = L.Class.extend({
this.tileLayerIds = options.tileLayerIds ? options.tileLayerIds : [];
this.zoomOffset = options.zoomOffset ? options.zoomOffset : 0;
this.vanillaOptions = options.vanillaOptions ? options.vanillaOptions : {};
this.vanillaMap = null;
this._overlayMaps = {};
this._tileLayerTitles = [];
this._vanillaMap = null;
this._vanillaLayersControl = null;
this._vanillaTileLayers = {};
},

_hasAnyLayer() {
let flag = false
for(const vanillaTileLayer of Object.values(this._vanillaTileLayers)) {
if(this._vanillaMap.hasLayer(vanillaTileLayer)) {
if(this.vanillaMap.hasLayer(vanillaTileLayer)) {
flag = true;
}
}
Expand Down
49 changes: 34 additions & 15 deletions src/multi-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ L.MultiMap = L.Class.extend({
return this._mapIds.includes(mapId)
},

getTileLayer: function (tileLayerId) {
for(const tileLayer of this.tileLayers) {
if (tileLayerId && tileLayer.id === tileLayerId) {
return tileLayer;
}
}
return null;
},

hasTileLayer: function (tileLayerId) {
if(!tileLayerId) return false;
for(const tileLayer of this.tileLayers) {
if(tileLayer.id === tileLayerId) {
return true;
}
}
return false;
},

/**
* Add a new map to the multi map
* @param map {L.MultiMap.Map}
Expand Down Expand Up @@ -128,7 +147,7 @@ L.MultiMap = L.Class.extend({
removeAllOverlays: function() {
for(const map of this.maps) {
for(const layer of Object.values(map._overlayMaps)) {
map._vanillaMap.removeLayer(layer);
map.vanillaMap.removeLayer(layer);
map._vanillaLayersControl.removeLayer(layer);
}
}
Expand All @@ -149,13 +168,13 @@ L.MultiMap = L.Class.extend({
mapElement.className = "leaflet-multi-map";
this._containerElement.appendChild(mapElement);

map._vanillaMap = L.map(map.id, this._getMapVanillaOptions(map));
map._vanillaMap.on("baselayerchange", this._onBaseLayerChange);
map._vanillaMap.on("overlayadd", this._onOverlayAdd);
map._vanillaMap.on("overlayremove", this._onOverlayRemove);
map._vanillaMap.on("moveend", (e) => this._onMoveEnd(e, map));
map.vanillaMap = L.map(map.id, this._getMapVanillaOptions(map));
map.vanillaMap.on("baselayerchange", this._onBaseLayerChange);
map.vanillaMap.on("overlayadd", this._onOverlayAdd);
map.vanillaMap.on("overlayremove", this._onOverlayRemove);
map.vanillaMap.on("moveend", (e) => this._onMoveEnd(e, map));

map._vanillaLayersControl = L.control.layers(map._vanillaTileLayers).addTo(map._vanillaMap);
map._vanillaLayersControl = L.control.layers(map._vanillaTileLayers).addTo(map.vanillaMap);
},

/**
Expand Down Expand Up @@ -209,9 +228,9 @@ L.MultiMap = L.Class.extend({
this._currentMapId = mapId;
this._currentMapIndex = this.maps.indexOf(map);
if(!map._hasAnyLayer() && map._tileLayerTitles.length > 0) {
map._vanillaMap.addLayer(map._vanillaTileLayers[map._tileLayerTitles[0]]);
map.vanillaMap.addLayer(map._vanillaTileLayers[map._tileLayerTitles[0]]);
}
map._vanillaMap.invalidateSize();
map.vanillaMap.invalidateSize();
}
}
},
Expand All @@ -226,7 +245,7 @@ L.MultiMap = L.Class.extend({
let name = overlay.name;
let l = cloneLayer(overlay.layer);
if(checked) {
map._vanillaMap.addLayer(l);
map.vanillaMap.addLayer(l);
}
if(name) {
map._vanillaLayersControl.addOverlay(l, name);
Expand Down Expand Up @@ -264,14 +283,14 @@ L.MultiMap = L.Class.extend({
_onOverlayAdd: function (event) {
for(const map of this.maps) {
let layer = map._overlayMaps[event.name];
map._vanillaMap.addLayer(layer);
map.vanillaMap.addLayer(layer);
}
},

_onOverlayRemove: function (event) {
for(const map of this.maps) {
const layer = map._overlayMaps[event.name];
map._vanillaMap.removeLayer(layer);
map.vanillaMap.removeLayer(layer);
}
},

Expand All @@ -282,17 +301,17 @@ L.MultiMap = L.Class.extend({
for(const otherMap of this.maps) {
if(otherMap.id !== this._currentMapId) {
const zoomOffset = otherMap.zoomOffset - currentZoomOffset;
otherMap._vanillaMap.setView(event.target.getCenter(), event.target.getZoom() - zoomOffset);
otherMap.vanillaMap.setView(event.target.getCenter(), event.target.getZoom() - zoomOffset);
}
}
},

_changeMapLayer: function (map, newLayerTitle) {
for (const [layerTitle, layer] of Object.entries(map._vanillaTileLayers)) {
if(layerTitle === newLayerTitle) {
map._vanillaMap.addLayer(layer);
map.vanillaMap.addLayer(layer);
} else {
map._vanillaMap.removeLayer(layer);
map.vanillaMap.removeLayer(layer);
}
}
},
Expand Down

0 comments on commit 2f68e9b

Please sign in to comment.