From aae16d2c1b7a12b700de936ced699cf16d473efe Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Fri, 6 Dec 2013 08:36:50 +0000 Subject: [PATCH] basemap-bing plugin: dynamic creation of bing map layer - in theory should reduce usage --- plugins/basemap-bing.user.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/plugins/basemap-bing.user.js b/plugins/basemap-bing.user.js index 43d0fea8..52944b49 100644 --- a/plugins/basemap-bing.user.js +++ b/plugins/basemap-bing.user.js @@ -2,7 +2,7 @@ // @id iitc-plugin-bing-maps // @name IITC plugin: Bing maps // @category Map Tiles -// @version 0.1.1.@@DATETIMEVERSION@@ +// @version 0.1.2.@@DATETIMEVERSION@@ // @namespace https://github.com/jonatkins/ingress-intel-total-conversion // @updateURL @@UPDATEURL@@ // @downloadURL @@DOWNLOADURL@@ @@ -23,7 +23,7 @@ window.plugin.mapBing = function() {}; window.plugin.mapBing.setupBingLeaflet = function() { @@INCLUDERAW:external/Bing.js@@ } - + window.plugin.mapBing.setup = function() { window.plugin.mapBing.setupBingLeaflet(); @@ -37,12 +37,35 @@ window.plugin.mapBing.setup = function() { 'AerialWithLabels': "Aerial with labels", }; + // bing maps has an annual usage limit, which will likely be hit in 6 months at this time. + // it seems that the usage is counted on initialising the L.BingLayer, when the metadata is retrieved. + // so, we'll create some dummy layers and add those to the map, then, the first time a layer is added, + // create the L.BingLayer. This will eliminate all usage for users who install but don't use the map, + // and only create usage for the map layers actually selected in use + + var bingMapContainers = []; + for (type in bingTypes) { var name = bingTypes[type]; - var bingMap = new L.BingLayer(bingApiKey, {type: type, maxZoom:20}); - layerChooser.addBaseLayer(bingMap, 'Bing '+name); + + bingMapContainers[type] = new L.LayerGroup(); + layerChooser.addBaseLayer(bingMapContainers[type], 'Bing '+name); } + // now a leaflet event to catch base layer changes and create a L.BingLayer when needed + map.on('baselayerchange', function(e) { + for (type in bingMapContainers) { + if (e.layer == bingMapContainers[type]) { + if (bingMapContainers[type].getLayers().length == 0) { + // dummy layer group is empty - create the bing layer + console.log('basemap-bing: creating '+type+' layer'); + var bingMap = new L.BingLayer (bingApiKey, {type: type, maxZoom:20}); + bingMapContainers[type].addLayer(bingMap); + } + } + } + }); + }; var setup = window.plugin.mapBing.setup;