简体   繁体   English

OpenLayers-获取几何投影

[英]OpenLayers - Get Geometry Projection

How can I get projection of a point or geometry in openlayers (2.12)? 如何在openlayers(2.12)中获得点或几何的投影?

for example: 例如:

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

and

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

appreciate any help 感谢任何帮助

In OpenLayers 2 it is the base map that has an associated projection. 在OpenLayers 2中,底图具有关联的投影。 If your base layer is a Google map, which inherits from SphericalMercator, the base layer will be EPSG:900913 aka EPSG:3857 . 如果您的基础层是继承自SphericalMercator的Google地图,则基础层将是EPSG:900913 aka EPSG:3857 If your base map is from some other service the projection may be WGS84 aka EPSG:4326 or it may be some other projection. 如果您的基本地图来自其他服务,则投影可能是WGS84 aka EPSG:4326或其他投影。

Later on in your code you are likely to need to determine the projection of the points you are getting in response to events so you will know if you need to project them to another coordinate reference frame. 稍后,在代码中,您可能需要确定响应事件而获得的点的投影,以便您知道是否需要将其投影到另一个坐标参考系。 One way to do that is: 一种方法是:

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);
    }
    ...

That way as recordCoord proceeds map_loc is now in WGS84, regardless of what it was before. 这样,随着recordCoord前进,map_loc现在在WGS84中,而不管它以前是什么。

If you have some other issue then I suggest adding some code to your question to show what you are trying to accomplish. 如果您还有其他问题,建议您在问题中添加一些代码,以显示您要完成的工作。

I can't get point projection by lat lon values, but solved it by adding projection property for each feature that will be added to the layer. 我无法通过纬度值获得点投影,但是可以通过为将添加到图层的每个要素添加投影属性来解决该问题。 my code is something like this: 我的代码是这样的:

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);

in first use, features projection set to wgs84, and then transform to spherical mercator. 在首次使用时,将特征投影设置为wgs84,然后转换为球形墨卡托。 for the next time use, does not change anything. 下次使用时,不会改变任何东西。

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

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