From 653a6cc82d802e82beabc90b968d12ba385543a5 Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Sun, 19 May 2013 15:29:36 +0100 Subject: [PATCH 1/2] rewrite default base map layer handling. it remembers by name, rather than id (so will work if order changes), and also works if plugins add additional base layers --- code/boot.js | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/code/boot.js b/code/boot.js index 7d5a45ec..1f4d9633 100644 --- a/code/boot.js +++ b/code/boot.js @@ -200,14 +200,6 @@ window.setupMap = function() { map.addControl(window.layerChooser); - // set the map AFTER adding the layer chooser, or Chrome reorders the - // layers. This likely leads to broken layer selection because the - // views/cookie order does not match the layer chooser order. - try { - convertCookieToLocalStorage('ingress.intelmap.type'); - map.addLayer(views[localStorage['ingress.intelmap.type']]); - } catch(e) { map.addLayer(views[0]); } - map.attributionControl.setPrefix(''); // listen for changes and store them in cookies map.on('moveend', window.storeMapPosition); @@ -228,10 +220,6 @@ window.setupMap = function() { console.log('Remove all resonators'); }); - map.on('baselayerchange', function () { - var selInd = $('[name=leaflet-base-layers]:checked').parent().index(); - localStorage['ingress.intelmap.type']=selInd; - }); // map update status handling & update map hooks // ensures order of calls @@ -248,6 +236,36 @@ window.setupMap = function() { }; +//adds a base layer to the map. done separately from the above, so that plugins that add base layers can be the default +window.setMapBaseLayer = function() { + //create a map name -> layer mapping - depends on internals of L.Control.Layers + var nameToLayer = {}; + var firstLayer = null; + + for (i in window.layerChooser._layers) { + var obj = window.layerChooser._layers[i]; + if (!obj.overlay) { + nameToLayer[obj.name] = obj.layer; + if (!firstLayer) firstLayer = obj.layer; + } + } + + var baseLayer = nameToLayer[localStorage['iitc-base-map']] || firstLayer; + map.addLayer(baseLayer); + + map.on('baselayerchange', function() { + for(i in window.layerChooser._layers) { + var obj = window.layerChooser._layers[i]; + if (!obj.overlay && map.hasLayer(obj.layer)) { + localStorage['iitc-base-map'] = obj.name; + break; + } + } + + }); + +} + // renders player details into the website. Since the player info is // included as inline script in the original site, the data is static // and cannot be updated. @@ -412,6 +430,8 @@ function boot() { } }); + window.setMapBaseLayer(); + window.runOnSmartphonesAfterBoot(); // workaround for #129. Not sure why this is required. From 7b574b9ea18a47a7a55ed8a4913bae8923fe5beb Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Sun, 19 May 2013 16:07:10 +0100 Subject: [PATCH 2/2] remove restrictions on zoom in getPosition. instead, after a defaault base layer is set, use setZoom(getZoom) to ensure zoom level is valid for this base map --- code/boot.js | 7 +++++++ code/location.js | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/code/boot.js b/code/boot.js index 1f4d9633..fd4d56ae 100644 --- a/code/boot.js +++ b/code/boot.js @@ -253,6 +253,12 @@ window.setMapBaseLayer = function() { var baseLayer = nameToLayer[localStorage['iitc-base-map']] || firstLayer; map.addLayer(baseLayer); + //after choosing a base layer, ensure the zoom is valid for this layer + //(needs to be done here - as we don't know the base layer zoom limit before this) + map.setZoom(map.getZoom()); + + + //event to track layer changes and store the name map.on('baselayerchange', function() { for(i in window.layerChooser._layers) { var obj = window.layerChooser._layers[i]; @@ -264,6 +270,7 @@ window.setMapBaseLayer = function() { }); + } // renders player details into the website. Since the player info is diff --git a/code/location.js b/code/location.js index efa85586..87ac0a27 100644 --- a/code/location.js +++ b/code/location.js @@ -25,7 +25,7 @@ window.getPosition = function() { var lat = parseInt(getURLParam('latE6'))/1E6 || 0.0; var lng = parseInt(getURLParam('lngE6'))/1E6 || 0.0; var z = parseInt(getURLParam('z')) || 17; - return {center: new L.LatLng(lat, lng), zoom: z > 18 ? 18 : z}; + return {center: new L.LatLng(lat, lng), zoom: z}; } if(getURLParam('ll')) { @@ -33,7 +33,7 @@ window.getPosition = function() { var lat = parseFloat(getURLParam('ll').split(",")[0]) || 0.0; var lng = parseFloat(getURLParam('ll').split(",")[1]) || 0.0; var z = parseInt(getURLParam('z')) || 17; - return {center: new L.LatLng(lat, lng), zoom: z > 18 ? 18 : z}; + return {center: new L.LatLng(lat, lng), zoom: z}; } if(readCookie('ingress.intelmap.lat') && readCookie('ingress.intelmap.lng')) { @@ -45,10 +45,10 @@ window.getPosition = function() { if(lat < -90 || lat > 90) lat = 0.0; if(lng < -180 || lng > 180) lng = 0.0; - return {center: new L.LatLng(lat, lng), zoom: z > 18 ? 18 : z}; + return {center: new L.LatLng(lat, lng), zoom: z}; } - setTimeout("window.map.locate({setView : true, maxZoom: 13});", 50); + setTimeout("window.map.locate({setView : true});", 50); return {center: new L.LatLng(0.0, 0.0), zoom: 1}; }