简体   繁体   中英

OpenLayers - Get Geometry Projection

How can I get projection of a point or geometry in openlayers (2.12)?

for example:

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

and

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

appreciate any help

In OpenLayers 2 it is the base map that has an associated projection. If your base layer is a Google map, which inherits from SphericalMercator, the base layer will be 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.

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.

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. for the next time use, does not change anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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