OpenLayers -获取几何投影



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

例如:

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

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

感谢您的帮助

在OpenLayers 2中,它是具有关联投影的底图。如果你的基础层是一个Google地图,它继承自SphericalMercator,基础层将是EPSG:900913又名EPSG:3857。如果您的底图来自其他服务,则投影可能是WGS84即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,然后转换为球形墨卡托。对于下次使用,不改变任何东西。

最新更新