简体   繁体   中英

How to Unproject and Reproject Vertices of a Polygon in openlayers with Google maps

I wanting to reproject the vertices of my polygon. I have been able to achieve this for the bounds but having a bit of trouble with the vertices.

           function onFeatureModified(event) {
           var bounds = event.feature.geometry.getBounds();
           var answer = "bottom: " + bounds.bottom + "\n";
           answer += "left: " + bounds.left + "\n";
           answer += "right: " + bounds.right + "\n";
           answer += "top: " + bounds.top + "\n";
           alert(answer);

           var mapProjv =  map.getProjectionObject();
           var epsg4326v = new OpenLayers.Projection("EPSG:4326");
       var verticesNative = event.features[0].geometry.getVertices();
       var verticesLatLon = verticesNative.transform( mapProjv , epsg4326v  );
       var vertices = verticesNative;
           alert(verticesNative);
           }

Below code that works:

           var vertices = event.features[0].geometry.getVertices();
           alert(vertices);

getVertices() method returns an array of OpenLayers.Geometry.Point objects, so you can't call transform() method on it. What you should do instead is to loop through array and call transform() on each object like that:

var vertices = event.features[0].geometry.getVertices();
var transformedVertices = [];
for(var i=0; i < vertices.length; i++){
    transformedVertices.push(vertices[i].transform(
        new OpenLayers.Projection("EPSG:900913"),
        new OpenLayers.Projection("EPSG:4326")
    ));
}

//create new feature with transformed vertices

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