如何获得经度和openlayers中的线的纬度

时间:2013-02-28 10:52:16

标签: javascript openlayers

我正在获得线路纬度&经度为

LINESTRING(1491215.4689647 6893983.2031826,1494163.0718675 6894785.7919795)

看到这个解决方案后。 how to get points return from OpenLayers.Control.DrawFeature

现在我想做的是我想要显示起点和点在我的网页上的终点。 那我怎样才能提取纬度和从这里经度,以便我可以在我的页面中显示它。

2 个答案:

答案 0 :(得分:0)

这就是WKT格式,你正在看。如果它们不在同一投影中,您可能需要将这些坐标重新投影到目标投影。之后,您应该能够使用基本几何函数每天向openlayers询问任何给定几何的点。从线串实例获取点数组并迭代它。确保您知道投影/数据模型的正确坐标顺序。

希望有所帮助!

答案 1 :(得分:0)

如果您的线串已经在OpenLayers中,则没有理由将其转换为WKT。 Linestring几何包含Points数组。您可以通过多种方式访问​​几何体的组件,例如:

drawControls[key].events.register('featureadded', drawControls[key], function(f) {

    // First point
    var firstPointGeom = f.feature.geometry.components[0].clone();

    // Last point
    var secondPointGeom = f.feature.geometry.components[f.feature.geometry.components.length - 1].clone();

    // Now you got geometries, let's create features from them...
    var firstPointFeat = new OpenLayers.Feature.Vector(firstPointGeom);
    var secondPointGeom = new OpenLayers.Feature.Vector(secondPointGeom);

    yourVectorLayer.addFeatures([firstPointFeat, secondPointGeom]);

});

注意 - 这适用于LineStrings。可能没有必要详细了解clone(),这取决于具体的用例,是否需要,或者只能使用var firstPointGeom = f.feature.geometry.components[0];