OpenLayers get Lat, Lon from polygon vertices

I am trying to get a border field using a square polygon in openlayers. I need to get North, South, West and East out of the box. Now I am using:

var topleft = vectors.features[0].geometry.getVertices()[0]; 

to get the top left vertex. However, it returns a value similar to this:

 POINT(-13393350.718762 4024321.5982824) 

How can I get the lat and lon values ​​from this returned point?

+4
source share
1 answer

You have one option for using getVertices () [i] to create a point

 var myPoint = new OpenLayers.Geometry.Point(vectors.features[0].geometry.getVertices()[0].x, vectors.features[0].geometry.getVertices()[0].y ) 

then convert this point to get Lat and Long with something like

 var myLatLonPoint = myPoint.transform( map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326")); 

Then you should be able to grab lat and long from these points.

Another option, it might be preferable to transform the border, and then pull out individual vertices.

 var myLatLonSquare = vectors.features[0].geometry.transform( map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326")); 

then pull out lat long vertices with:

 myLatLonSquare.getVertices()[0].x myLatLonSquare.getVertices()[0].y 
+6
source

Source: https://habr.com/ru/post/1385374/


All Articles