Merge pull request #700 from rjmackay/bing-maps-ssl-699

Make Bing maps use SSL (if current document does)
This commit is contained in:
ahocevar
2012-12-13 16:23:54 -08:00
2 changed files with 54 additions and 2 deletions

View File

@@ -58,6 +58,12 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
* {Object} Metadata for this layer, as returned by the callback script
*/
metadata: null,
/**
* Property: protocolRegex
* {RegExp} Regular expression to match and replace http: in bing urls
*/
protocolRegex: /^http:/i,
/**
* APIProperty: type
@@ -91,6 +97,19 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
*/
tileOptions: null,
/** APIProperty: protocol
* {String} Protocol to use to fetch Imagery Metadata, tiles and bing logo
* Can be 'http:' 'https:' or ''
*
* Warning: tiles may not be available under both HTTP and HTTPS protocols.
* Microsoft approved use of both HTTP and HTTPS urls for tiles. However
* this is undocumented and the Imagery Metadata API always returns HTTP
* urls.
*
* Default is
*/
protocol: '',
/**
* Constructor: OpenLayers.Layer.Bing
* Create a new Bing layer.
@@ -145,7 +164,7 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
jsonp: this._callbackId,
include: "ImageryProviders"
}, this.metadataParams);
var url = "//dev.virtualearth.net/REST/v1/Imagery/Metadata/" +
var url = this.protocol + "//dev.virtualearth.net/REST/v1/Imagery/Metadata/" +
this.type + "?" + OpenLayers.Util.getParameterString(params);
var script = document.createElement("script");
script.type = "text/javascript";
@@ -163,6 +182,7 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
var res = this.metadata.resourceSets[0].resources[0];
var url = res.imageUrl.replace("{quadkey}", "${quadkey}");
url = url.replace("{culture}", this.culture);
url = url.replace(this.protocolRegex, this.protocol);
this.url = [];
for (var i=0; i<res.imageUrlSubdomains.length; ++i) {
this.url.push(url.replace("{subdomain}", res.imageUrlSubdomains[i]));
@@ -248,9 +268,10 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
}
}
}
var logo = metadata.brandLogoUri.replace(this.protocolRegex, this.protocol);
this.attribution = OpenLayers.String.format(this.attributionTemplate, {
type: this.type.toLowerCase(),
logo: metadata.brandLogoUri,
logo: logo,
copyrights: copyrights
});
this.map && this.map.events.triggerEvent("changelayer", {

View File

@@ -178,6 +178,37 @@
t.ok(clone instanceof OpenLayers.Layer.Bing, "clone is a Layer.Bing instance");
}
function test_protocol(t)
{
t.plan(5);
var map = new OpenLayers.Map("map");
layer = new OpenLayers.Layer.Bing(options);
map.addLayer(layer);
map.zoomToMaxExtent();
var tile = layer.tileQueue[0];
t.delay_call(5, function() {
t.ok(OpenLayers.Util.indexOf(layer.attribution, '<img src="//') != -1, "Attribution contains a logo with protocol //");
t.ok(OpenLayers.Util.indexOf(layer.attribution, '<img src="http://') == -1, "Attribution logo does not have http:// protocol");
t.ok(OpenLayers.Util.indexOf(tile.url, 'http:') == -1, "Tile url does not contain http:");
map.destroy();
});
var map2 = new OpenLayers.Map("map");
layer_https = new OpenLayers.Layer.Bing(OpenLayers.Util.applyDefaults({protocol: 'https:'}, options));
map2.addLayer(layer_https);
map2.zoomToMaxExtent();
var tile = layer_https.tileQueue[0];
t.delay_call(5, function() {
t.ok(OpenLayers.Util.indexOf(layer_https.attribution, '<img src="https://') != -1, "Attribution logo has https:// protocol");
t.ok(OpenLayers.Util.indexOf(tile.url, 'https:') == 0, "Tile url contains https:");
map2.destroy();
});
}
</script>
</head>
<body>