我在谷歌地图上有这个图层:(注意getTileUrl函数中的generateAPIKey()函数)
var options ={
center:new google.maps.LatLng(somelat,somelon),
zoom:14,
disableDefaultUI:true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
minZoom:11,
maxZoom:16,
};
var map = new google.maps.Map(document.getElementById('map_canvas'),options);
var myLayerOptions = {
getTileUrl: function(coord, zoom) {
return 'http://localhost/mylayer/'+zoom+'/'+coord.x+'/'+coord.y+'.png&apikey='+ generateAPIKey();
},
tileSize: new google.maps.Size(256, 256),
isPng: true,
opacity:1.0
};
var myLayerMapType = new google.maps.ImageMapType(myLayerOptions);
map.overlayMapTypes.insertAt(0, myLayerMapType);
所以我的主要问题是我有一个动态API密钥用于我正在使用的服务,使用开放层我有这个:
var mylayer = new OpenLayers.Layer.XYZ(
"mylayer",
[
"http://localhost/mylayer/${z}/${x}/${y}.png&apikey='+ generateAPIKey()
], {
sphericalMercator: true,
wrapDateLine: true,
transitionEffect: "resize",
buffer: 1,
numZoomLevels: 17
}
);
var map = new OpenLayers.Map({
div: "map_canvas",
layers: [mylayer],
controls: [
new OpenLayers.Control.Attribution(),
new OpenLayers.Control.Navigation({
dragPanOptions: {
enableKinetic: true
}
}),
new OpenLayers.Control.Zoom(),
new OpenLayers.Control.Permalink({anchor: true})
],
center: [somelat,somelon],
zoom: 14
});
我明白什么是错的,generateAPIKey函数只执行一次。是否有类似Google getTileUrl的内容?
答案 0 :(得分:2)
是的,XYZ层的方法getURL
可以为每个区块调用,如下所示:
/**
* Method: getURL
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
*
* Returns:
* {String} A string with the layer's url and parameters and also the
* passed-in bounds and appropriate tile size specified as
* parameters
*/
getURL: function (bounds) {
var xyz = this.getXYZ(bounds);
var url = this.url;
if (OpenLayers.Util.isArray(url)) {
var s = '' + xyz.x + xyz.y + xyz.z;
url = this.selectUrl(s, url);
}
return OpenLayers.String.format(url, xyz);
}
只需覆盖此方法并附加您的API密钥。