分享

Saving and restoring geometries in OpenLayers

 LibraryPKU 2014-05-14

To convert a vector layer or an Openlayers.Geometry object to a GeoJSON string, you should use the OpenLayers.Format.GeoJSON.write function:

var geoJSON = new OpenLayers.Format.GeoJSON(),
    geoJSONText = geoJSON.write(geometryObject);

Note that you should be able to pass your object to this function, since (according to documentation) it accepts an OpenLayers.Feature.Vector as well as a OpenLayers.Geometry or an array of features.

Conversely, when you’ve got a GeoJSON string, you can convert that back to an object using the OpenLayers.Format.GeoJSON.read function:

var geometry = geoJSON.read(geoJSONText, 'Geometry');

The second parameter lets you indicate which type of object you’d like returned. Read the docs linked to for more information.

Also, take a look at this demo for a more extensive example. (View the source of the page to see how they’re doing it).

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多