0

我已经尝试从以前在 SE 中提出的问题中解决问题,但无法通过阅读这些问题来解决问题,因此,作为单独的问题发布。

我正在marker使用以下代码创建一个图层

mLayer = new OpenLayers.Layer.Vector(layerName, {
        projection: map.displayProjection,
        extractAttributes: true,
        styleMap: new OpenLayers.StyleMap({'default' : Lstyle}),
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: getURL,
            format: new OpenLayers.Format.GeoJSON()
        })
    });

我的任务是计算marker坐标的平均值并将其设置为地图的中心,因为我想从mLayer对象中获取坐标。标记在地图上显示良好,但是当我mLayer.features.length在控制台中打印时它显示0但大小features为 10,我可以firebug console通过打印看到vLayer是我的 JSON 文件。

4

2 回答 2

1

Openlayers 提供了这个功能:

然后使用map.getZoomForExtentmap.panTo

于 2013-02-13T15:52:36.520 回答
0

将地图中心设置markers mid pointChristophe Roussy建议的在comments某种程度上是正确的,但他的代码的问题是,代码会在layer完成下载到浏览器之前执行,所以我们必须检查图层是否完成如下

layer.events.register('loadend', layer, function(evt){map.zoomToExtent(layer.getDataExtent() )} )

它对我来说工作正常。

于 2013-02-14T12:02:50.457 回答