|
|
|
/*
|
|
|
|
* Copyright 2020 Netsyms Technologies.
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var map = null;
|
|
|
|
|
|
|
|
var maptype = "mapbox";
|
|
|
|
|
|
|
|
function createMap() {
|
|
|
|
if (getStorage("maptype") == null) {
|
|
|
|
setStorage("maptype", "mapbox");
|
|
|
|
}
|
|
|
|
maptype = getStorage("maptype");
|
|
|
|
if (maptype == "mapbox") {
|
|
|
|
if (mapboxgl.supported()) {
|
|
|
|
map = mapboxMap();
|
|
|
|
} else {
|
|
|
|
console.log("Warn", "mapbox-gl not supported, falling back to Leaflet");
|
|
|
|
maptype = "leaflet";
|
|
|
|
map = leafletMap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
map = leafletMap();
|
|
|
|
}
|
|
|
|
map.updateUserLayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy and re-create the map.
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
function reloadMap() {
|
|
|
|
try {
|
|
|
|
if (map != null && typeof map != 'undefined') {
|
|
|
|
var mapcenter = map.getCenter();
|
|
|
|
var mapzoom = map.getZoom();
|
|
|
|
if (map.maptype == "mapbox") {
|
|
|
|
var mapbearing = map.getBearing();
|
|
|
|
var mappitch = map.getPitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
map.off();
|
|
|
|
map.remove();
|
|
|
|
map = null;
|
|
|
|
|
|
|
|
if (document.getElementById("mapbox") != null) {
|
|
|
|
createMap();
|
|
|
|
|
|
|
|
if (map.maptype == "mapbox") {
|
|
|
|
map.jumpTo({
|
|
|
|
center: mapcenter,
|
|
|
|
zoom: mapzoom,
|
|
|
|
bearing: mapbearing,
|
|
|
|
pitch: mappitch
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
map.setView(mapcenter, mapzoom);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("Info", "Not re-creating map because #mapbox is not in DOM. Creation will be automatically triggered when map page is loaded.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
createMap();
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
// oh well ¯\(°_o)/¯
|
|
|
|
console.log(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setMapLocation(latitude, longitude) {
|
|
|
|
if (map == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
map.setMapLocation(latitude, longitude);
|
|
|
|
}
|
|
|
|
|
|
|
|
function animateMapIn(latitude, longitude, zoom, heading) {
|
|
|
|
if (map == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof zoom == 'undefined') {
|
|
|
|
zoom = 14;
|
|
|
|
}
|
|
|
|
if (typeof heading == 'undefined') {
|
|
|
|
heading = 0;
|
|
|
|
}
|
|
|
|
map.animateMapIn(latitude, longitude, zoom, heading);
|
|
|
|
}
|