繁体   English   中英

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 aka EPSG:3857 如果您的基本地图来自其他服务,则投影可能是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,然后转换为球形墨卡托。 下次使用时,不会改变任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM