finalized BingLayers;

added culture support outside of BingLayers.js
This commit is contained in:
DennisSchiefer 2012-08-13 13:29:03 +01:00
parent 0916e3f2e3
commit e8eb342434
17 changed files with 194 additions and 199 deletions

View File

@ -20,6 +20,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
// will hold the map object
OSRM.GLOBALS.map = null;
OSRM.GLOBALS.localizable_maps = [];
// map controller
@ -38,6 +39,7 @@ init: function() {
for(var i=0, size=tile_servers.length; i<size; i++) {
if( tile_servers[i].bing == true ) {
base_maps[ tile_servers[i].display_name ] = new L.BingLayer( tile_servers[i].apikey, tile_servers[i].options );
OSRM.G.localizable_maps.push( base_maps[ tile_servers[i].display_name ] );
} else {
tile_servers[i].options.attribution = tile_servers[i].attribution;
base_maps[ tile_servers[i].display_name ] = new L.TileLayer( tile_servers[i].url, tile_servers[i].options );

View File

@ -0,0 +1,145 @@
/*
Copyright (c) 2011-2012, Pavel Shramov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// TileLayer using BingMaps
// [modified to support multiple cultures]
L.BingLayer = L.TileLayer.extend({
options: {
subdomains: [0, 1, 2, 3],
type: 'Aerial', // supported: 'Road', 'Aerial', 'AerialWithLabels'
attribution: 'Bing',
culture: 'en-US'
},
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[(p.x + p.y) % subdomains.length];
return this._url.replace('{subdomain}', s)
.replace('{quadkey}', this.tile2quad(p.x, p.y, z))
.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 = "http://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.addAttribution(p.attrib);
p.active = true;
} else {
if (p.active)
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.removeAttribution(p.attrib);
p.active = false;
}
}
L.TileLayer.prototype.onRemove.apply(this, [map]);
}
});

View File

@ -1,7 +1,35 @@
/*
Copyright (c) 2011-2012, Pavel Shramov
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// TileLayer using BingMaps
// [modified to support multiple cultures]
L.BingLayer = L.TileLayer.extend({
options: {
subdomains: [0, 1, 2, 3],
type: 'Aerial',
type: 'Aerial', // supported: 'Road', 'Aerial', 'AerialWithLabels'
attribution: 'Bing',
culture: 'en-US'
},
@ -115,197 +143,3 @@ L.BingLayer = L.TileLayer.extend({
L.TileLayer.prototype.onRemove.apply(this, [map]);
}
});
/*
* Portions of this code and logic copied from OpenLayers and
* redistributed under the original Clear BSD license terms:
*
* http://trac.osgeo.org/openlayers/browser/license.txt
*
* Copyright 2005-2010 OpenLayers Contributors, released under
* the Clear BSD license. See authors.txt for a list of contributors.
* All rights reserved.
*
* --
*
* Leaflet-specific modifications are released under the following
* terms:
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
L.TileLayer.Bing = L.TileLayer.extend({
supportedTypes: ['Road', 'Aerial', 'AerialWithLabels'],
attributionTemplate: '<span style="display:inline-block">' +
'<a target="_blank" href="http://www.bing.com/maps/">' +
//'<img src="{logo}" /></a><br><span>{copyrights}' +
'</a><span>{copyrights}' +
'<a style="white-space: nowrap" target="_blank" '+
'href="http://www.microsoft.com/maps/product/terms.html">' +
'Terms of Use</a></span></span>',
supportedCultures: {"en":"en-US", "de":"de-DE", "fr":"fr-FR", "it":"it-IT", "es":"es-ES", "nl":"nl-BE"},
initialize: function(/*String*/ apiKey, /*String*/ mapType, /*Object*/ options) {
this._apiKey = apiKey;
this._mapType = mapType;
this._loadMetadata();
L.Util.setOptions(this, options);
},
redraw: function() {
this._reset();
this._update();
},
_loadMetadata: function() {
this._callbackId = "_l_tilelayer_bing_" + (L.TileLayer.Bing._callbackId++);
var that = this;
window[this._callbackId] = function() {
L.TileLayer.Bing.processMetadata.apply(that, arguments);
};
var params = {
key: this._apiKey,
jsonp: this._callbackId,
include: 'ImageryProviders'
},
url = "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/" +
this._mapType + L.Util.getParamString(params),
script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
script.id = this._callbackId;
document.getElementsByTagName("head")[0].appendChild(script);
},
_onMetadataLoaded: function() {},
onAdd: function(map, insertAtTheBottom) {
if (!this.metadata) {
this._onMetadataLoaded = L.Util.bind(function() {
L.TileLayer.prototype.onAdd.call(this, map, insertAtTheBottom);
map.on('moveend', this._updateAttribution, this);
this._updateAttribution();
}, this);
} else {
L.TileLayer.prototype.onAdd.call(this, map, insertAtTheBottom);
map.on('moveend', this._updateAttribution, this);
this._updateAttribution();
}
},
onRemove: function(map) {
if (this._map.attributionControl) {
this._map.attributionControl.removeAttribution(this.attribution);
}
this._map.off('moveend', this._updateAttribution, this);
L.TileLayer.prototype.onRemove.call(this, map);
},
getTileUrl: function(xy, z) {
var subdomains = this.options.subdomains,
quadDigits = [],
i = z,
digit,
mask,
quadKey;
// borrowed directly from OpenLayers
for (; i > 0; --i) {
digit = '0';
mask = 1 << (i - 1);
if ((xy.x & mask) != 0) {
digit++;
}
if ((xy.y & mask) != 0) {
digit++;
digit++;
}
quadDigits.push(digit);
}
return this._url
.replace('{culture}', this.supportedCultures[OSRM.Localization.current_language] || "en-US" )
.replace('{subdomain}', subdomains[(xy.x + xy.y) % subdomains.length])
.replace('{quadkey}', quadDigits.join(""));
},
_updateAttribution: function() {
if (this._map.attributionControl) {
var metadata = this.metadata;
var res = metadata.resourceSets[0].resources[0];
var bounds = this._map.getBounds();
var providers = res.imageryProviders, zoom = this._map.getZoom() + 1,
copyrights = "", provider, i, ii, j, jj, bbox, coverage;
for (i=0,ii=providers.length; i<ii; ++i) {
provider = providers[i];
for (j=0,jj=provider.coverageAreas.length; j<jj; ++j) {
coverage = provider.coverageAreas[j];
if (zoom <= coverage.zoomMax && zoom >= coverage.zoomMin && coverage.bbox.intersects(bounds)) {
copyrights += provider.attribution + " ";
j = jj;
}
}
}
this._map.attributionControl.removeAttribution(this.attribution);
this._map.attributionControl._attributions = {};
this._map.attributionControl._update();
this.attribution = this.attributionTemplate
.replace('{logo}', metadata.brandLogoUri)
.replace('{copyrights}', copyrights);
this._map.attributionControl.addAttribution(this.attribution);
}
}
});
L.TileLayer.Bing._callbackId = 0;
L.TileLayer.Bing.processMetadata = function(metadata) {
if (metadata.authenticationResultCode != 'ValidCredentials') {
throw "Invalid Bing Maps API Key"
}
if (!metadata.resourceSets.length || !metadata.resourceSets[0].resources.length) {
throw "No resources returned, perhaps " + this._mapType + " is an invalid map type?";
}
if (metadata.statusCode != 200) {
throw "Bing Maps API request failed with status code " + metadata.statusCode;
}
this.metadata = metadata;
var res = metadata.resourceSets[0].resources[0],
providers = res.imageryProviders,
i = 0,
j,
provider,
bbox,
script = document.getElementById(this._callbackId);
for (; i<providers.length; i++) {
provider = providers[i];
for (j=0; j<provider.coverageAreas.length; j++) {
bbox = provider.coverageAreas[j].bbox;
provider.coverageAreas[j].bbox = new L.LatLngBounds(new L.LatLng(bbox[0],bbox[1],true),new L.LatLng(bbox[2],bbox[3], true));
}
}
this._url = res.imageUrl;
this.options.subdomains = [].concat(res.imageUrlSubdomains);
script.parentNode.removeChild(script);
window[this._callbackId] = undefined; // cannot delete from window in IE
delete this._callbackId;
this._onMetadataLoaded();
}

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["de"] = {
// own language
"CULTURE": "de-DE",
"LANGUAGE": "Deutsch",
//gui
"GUI_START": "Start",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["dk"] = {
// own language
"CULTURE": "da-DK",
"LANGUAGE": "Dansk",
//gui
"GUI_START": "Start",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["en"] = {
// own language
"CULTURE": "en-US",
"LANGUAGE": "English",
// gui
"GUI_START": "Start",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["es"] = {
// own language
"CULTURE": "es-ES",
"LANGUAGE": "Español",
// gui
"GUI_START": "Inicio",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["fi"] = {
// own language
"CULTURE": "fi-FI",
"LANGUAGE": "Suomi",
//gui
"GUI_START": "Lähtöpaikka",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["fr"] = {
// own language
"CULTURE": "fr-FR",
"LANGUAGE": "Français",
//gui
"GUI_START": "Départ",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["it"] = {
// own language
"CULTURE": "it-IT",
"LANGUAGE": "Italiano",
//gui
"GUI_START": "Partenza",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["lv"] = {
// own language
"CULTURE": "lv-LV",
"LANGUAGE": "Latviešu",
// gui
"GUI_START": "Sākums",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["pl"] = {
// own language
"CULTURE": "pl-PL",
"LANGUAGE": "Polski",
//gui
"GUI_START": "Początek",

View File

@ -21,6 +21,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.Localization["ru"] = {
// own language
"CULTURE": "ru-RU",
"LANGUAGE": "Русский",
// gui
"GUI_START": "Начало",

View File

@ -62,6 +62,9 @@ setLanguage: function(language) {
// change gui language
OSRM.GUI.setLabels();
// change map language
for(var i=0, size=OSRM.G.localizable_maps.length; i<size; i++) {
OSRM.G.localizable_maps[i].options.culture = OSRM.loc("CULTURE");
}
if(OSRM.G.map.layerControl.getActiveLayer().redraw)
OSRM.G.map.layerControl.getActiveLayer().redraw();
// requery data

View File

@ -41,7 +41,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="base/leaflet/L.DashedPolyline.js" type="text/javascript"></script>
<script src="base/leaflet/L.MouseMarker.js" type="text/javascript"></script>
<script src="base/leaflet/L.SwitchableIcon.js" type="text/javascript"></script>
<script src="base/leaflet/L.TileLayer.Bing.js" type="text/javascript"></script>
<script src="base/leaflet/L.BingLayer.js" type="text/javascript"></script>
<script src="OSRM.base.js" type="text/javascript"></script>
<script src="OSRM.config.js" type="text/javascript"></script>

View File

@ -41,7 +41,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="../base/leaflet/L.DashedPolyline.js" type="text/javascript"></script>
<script src="../base/leaflet/L.MouseMarker.js" type="text/javascript"></script>
<script src="../base/leaflet/L.SwitchableIcon.js" type="text/javascript"></script>
<script src="../base/leaflet/L.TileLayer.Bing.js" type="text/javascript"></script>
<script src="../base/leaflet/L.BingLayer.js" type="text/javascript"></script>
<script src="../base/osrm/OSRM.MapView.js" type="text/javascript"></script>
</head>

View File

@ -55,8 +55,9 @@ OSRM.prefetchIcons = function(images_list) {
OSRM.drawMap = function(tile_server, bounds) {
// setup map
var tile_layer;
if( tile_server.bing ) tile_layer = new L.TileLayer.Bing(tile_server.apikey, tile_server.type, tile_server.options);
if( tile_server.bing ) tile_layer = new L.BingLayer(tile_server.apikey, tile_server.options);
else tile_layer = new L.TileLayer(tile_server.url, tile_server.options);
tile_layer.options.culture = OSRM.loc("CULTURE");
OSRM.G.map = new OSRM.MapView("overview-map", {
center: new L.LatLng(48.84, 10.10),
zoom: 13,