From aad7292c68b9bf95ab79c3cccaa3a21e4329add6 Mon Sep 17 00:00:00 2001 From: Arvind Date: Fri, 6 Sep 2013 14:17:36 +0530 Subject: [PATCH] Satellite imagery from Bing --- Bing.js | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 2 +- main.js | 11 ++++-- 3 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 Bing.js diff --git a/Bing.js b/Bing.js new file mode 100644 index 0000000..ac29d5f --- /dev/null +++ b/Bing.js @@ -0,0 +1,122 @@ +L.BingLayer = L.TileLayer.extend({ + options: { + subdomains: [0, 1, 2, 3], + type: 'Aerial', + attribution: 'Bing', + culture: '' + }, + + initialize: function(key, options) { + L.Util.setOptions(this, options); + + this._key = key; + this._url = null; + this.meta = {}; + this.loadMetadata(); + }, + + tile2quad: function(x, y, z) { + var quad = ''; + for (var i = z; i > 0; i--) { + var digit = 0; + var mask = 1 << (i - 1); + if ((x & mask) != 0) digit += 1; + if ((y & mask) != 0) digit += 2; + quad = quad + digit; + } + return quad; + }, + + getTileUrl: function(p, z) { + var z = this._getZoomForUrl(); + var subdomains = this.options.subdomains, + s = this.options.subdomains[Math.abs((p.x + p.y) % subdomains.length)]; + return this._url.replace('{subdomain}', s) + .replace('{quadkey}', this.tile2quad(p.x, p.y, z)) + .replace('http:', document.location.protocol) + .replace('{culture}', this.options.culture); + }, + + loadMetadata: function() { + var _this = this; + var cbid = '_bing_metadata_' + L.Util.stamp(this); + window[cbid] = function (meta) { + _this.meta = meta; + window[cbid] = undefined; + var e = document.getElementById(cbid); + e.parentNode.removeChild(e); + if (meta.errorDetails) { + alert("Got metadata" + meta.errorDetails); + return; + } + _this.initMetadata(); + }; + var url = document.location.protocol + "//dev.virtualearth.net/REST/v1/Imagery/Metadata/" + this.options.type + "?include=ImageryProviders&jsonp=" + cbid + "&key=" + this._key; + var script = document.createElement("script"); + script.type = "text/javascript"; + script.src = url; + script.id = cbid; + document.getElementsByTagName("head")[0].appendChild(script); + }, + + initMetadata: function() { + var r = this.meta.resourceSets[0].resources[0]; + this.options.subdomains = r.imageUrlSubdomains; + this._url = r.imageUrl; + this._providers = []; + for (var i = 0; i < r.imageryProviders.length; i++) { + var p = r.imageryProviders[i]; + for (var j = 0; j < p.coverageAreas.length; j++) { + var c = p.coverageAreas[j]; + var coverage = {zoomMin: c.zoomMin, zoomMax: c.zoomMax, active: false}; + var bounds = new L.LatLngBounds( + new L.LatLng(c.bbox[0]+0.01, c.bbox[1]+0.01), + new L.LatLng(c.bbox[2]-0.01, c.bbox[3]-0.01) + ); + coverage.bounds = bounds; + coverage.attrib = p.attribution; + this._providers.push(coverage); + } + } + this._update(); + }, + + _update: function() { + if (this._url == null || !this._map) return; + this._update_attribution(); + L.TileLayer.prototype._update.apply(this, []); + }, + + _update_attribution: function() { + var bounds = this._map.getBounds(); + var zoom = this._map.getZoom(); + for (var i = 0; i < this._providers.length; i++) { + var p = this._providers[i]; + if ((zoom <= p.zoomMax && zoom >= p.zoomMin) && + bounds.intersects(p.bounds)) { + if (!p.active && this._map.attributionControl) + this._map.attributionControl.addAttribution(p.attrib); + p.active = true; + } else { + if (p.active && this._map.attributionControl) + this._map.attributionControl.removeAttribution(p.attrib); + p.active = false; + } + } + }, + + onRemove: function(map) { + for (var i = 0; i < this._providers.length; i++) { + var p = this._providers[i]; + if (p.active && this._map.attributionControl) { + this._map.attributionControl.removeAttribution(p.attrib); + p.active = false; + } + } + L.TileLayer.prototype.onRemove.apply(this, [map]); + } +}); + +L.bingLayer = function (key, options) { + return new L.BingLayer(key, options); +}; diff --git a/index.html b/index.html index 970a717..a55313a 100644 --- a/index.html +++ b/index.html @@ -13,7 +13,7 @@ - + diff --git a/main.js b/main.js index 4ba4a67..45ae00f 100644 --- a/main.js +++ b/main.js @@ -7,16 +7,17 @@ function init(){ maxZoom: 18, attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery ©CloudMade' }); - var sat = L.tileLayer.provider("MapQuestOpen.Aerial"); + // var sat = L.tileLayer.provider("MapQuestOpen.Aerial"); + var bing = new L.BingLayer("AjuZVKHlyC5_471vh2TFunhEtLhMF9tI6TNBQddvUK0lpMJXrJmFBlCssPC68WxS"); var overlays = { "Places" : markers, "Photos": photos }; var baseLayer = { "Base Map": tileLayer, - "Satellite": sat + "Satellite": bing }; - map = L.map('map', {layers:[tileLayer, markers, photos, sat]}).setView([14.31336, 76.64973], 10); + map = L.map('map', {layers:[tileLayer, markers, photos, bing]}).setView([14.31336, 76.64973], 12); map.minZoom = 10; map.maxZoom = 13; map.zoomControl = true; @@ -77,4 +78,6 @@ function loadTrackPoints() { }).addTo(map); },'json'); } -} \ No newline at end of file +} + + -- 1.7.10.4