0

我在谷歌地图上有这一层:(注意 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 的东西吗?

4

1 回答 1

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 密钥。

于 2012-06-08T07:05:04.020 回答