OpenLayers - 获取几何投影

OpenLayers - Get Geometry Projection

如何在 openlayers (2.12) 中获取点或几何的投影?

例如:

x = 30.453789 , y = 35.637485 ==> EPSG:4326

x = 3667550.3453 , y = 2205578.3453 ==> EPSG:900913

感谢任何帮助

在 OpenLayers 2 中,它是具有关联投影的底图。如果您的基础层是继承自 SphericalMercator 的 Google 地图,则基础层将是 EPSG:900913 又名 EPSG:3857 . If your base map is from some other service the projection may be WGS84 aka EPSG:4326 或者它可能是其他投影。

稍后在您的代码中,您可能需要确定您为响应事件而获得的点的投影,以便您知道是否需要将它们投影到另一个坐标参考系。一种方法是:

WGS84 = new OpenLayers.Projection("EPSG:4326"),
...

// Register event handler
map_layers.point.events.on({
  beforefeatureadded: recordCoord,
  featuremodified: recordCoord,
  afterfeaturemodified: recordCoord,
  featureselected: recordCoord,
  featureunselected: recordCoord,
  vertexmodified: recordCoord
});

...
// Handler to capture map additions/modifications/etc.
function recordCoord(event) {
    var layer = this,
        geometry = event.feature.geometry,
        map_loc = new OpenLayers.LonLat(geometry.x, geometry.y);
    if (map.getProjection() !== WGS84.getCode()) {
        map_loc.transform(map.getProjectionObject(), WGS84);
    }
    ...

随着 recordCoord 的进行,map_loc 现在在 WGS84 中,不管它以前是什么。

如果您有其他问题,那么我建议您在问题中添加一些代码以显示您要完成的任务。

我无法通过经纬度值获得点投影,但通过为将添加到图层的每个要素添加投影 属性 解决了这个问题。我的代码是这样的:

var mapProjection = new OpenLayers.Projection("EPSG:900913");
var dbProjection = new OpenLayers.Projection("EPSG:4326");

layer.preFeatureInsert = function (feature) {
    if (!feature.projection)
        feature.projection = dbProjection;

    if (feature.projection != mapProjection)
        feature.geometry.transform(feature.projection, mapProjection);

    //do something...
}
map.addLayer(layer);

第一次使用,特征投影设置为wgs84,然后变换为球面墨卡托。下次使用,不改变任何东西。