gberg927
gberg927

Reputation: 1686

OpenLayers get Lat, Lon from polygon vertices

I am trying to get the boundary box using a square polygon in openlayers. I need to get the North, South, West, and East values from the box. Right now I am using :

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

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

POINT(-13393350.718762 4024321.5982824)

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

Upvotes: 4

Views: 6597

Answers (1)

AlexC
AlexC

Reputation: 10756

One option you have is using the getVertices()[i] to generate a point

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

then transform that 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 the lat and long from those points.

Another option, possibly preferable, would be to transform the boundary and then pull out the individual vertices.

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

then pull out the lat long of the vertices with:

myLatLonSquare.getVertices()[0].x  myLatLonSquare.getVertices()[0].y

Upvotes: 6

Related Questions