【问题标题】:how to adjust center and zoom level according to the geojson in openlayers 2如何根据openlayers 2中的geojson调整中心和缩放级别
【发布时间】:2014-07-09 13:12:24
【问题描述】:

当我们显示 GeoJson 时,它就像在 Leaflet 中一样完美定位和缩放(例如http://www.geojsonlint.com/)如何在 OpenLayers2 中做同样的事情?

【问题讨论】:

    标签: openlayers gis


    【解决方案1】:

    您从要素的几何图形中获取边界(即,由 GeoJSON 格式读取器返回的内容),然后调用 map.zoomToExtents(bounds, nearest)。最接近的参数决定了它与多边形范围的缩放距离。

    var geojson_format = new OpenLayers.Format.GeoJSON();
    var vectors = new OpenLayers.Layer.Vector();     
    map.addLayer(vectors);  
    var feature=geojson_format.read(json); 
    vectors.addFeatures(feature);    
    var bounds=feature[0].geometry.getBounds();
    map.zoomToExtent(bounds, false);
    

    这是一个jsFiddle 展示它的实际应用。

    【讨论】:

    • @AnushreeAcharjee。我已经用一个工作的 jsFiddle 更新了帖子。
    • 谢谢约翰:)你是一个救生员。
    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多