[OpenLayers-Users] Calculating Distance between two points

Leipold, Christopher leipold at bi.bisnode.de
Fri Jun 25 04:07:22 EDT 2010


Hello,

I'm trying to build a function that lets me draw a 'circle' (40-sided
polygon) and then get the coordinates of it's center and get it's
radius.

Drawing the circle is pretty straightforward (I just copied the code
from http://openlayers.org/dev/examples/regular-polygons.html). Getting
the Centroid and handling the sketchcomplete-event works (with Fabian's
help:
http://osgeo-org.1803224.n2.nabble.com/Get-infos-on-newly-created-polygo
n-with-Draw-Feature-td5216851.html#a5216851).

I'm still stuck with getting the radius. I'm using a random point on the
circle with

var aussen = new
OpenLayers.Geometry.Point(event.feature.geometry.components[0].component
s[0].x,event.feature.geometry.components[0].components[0].y);

(though I'm not sure if that's the best way). And try to use
getGeodesicLength to get the distance (I need it in meters/km).

I tried this:

 

function getRingCenter(event) {

  var proj = map.getProjectionObject();

  var mitte = event.feature.geometry.getCentroid();

  var aussen = new
OpenLayers.Geometry.Point(event.feature.geometry.components[0].component
s[0].x,event.feature.geometry.components[0].components[0].y);

  var line = new OpenLayers.Geometry.LineString([mitte, aussen]);

 

  alert (line.getGeodesicLength(proj));

 

  alert (line.getGeodesicLength(proj)* this.map.getScale() );

  alert (this.map.getUnits());

}

 

I don't understand what I get in the first alert, but it seems that it
has to do with the current display size/zoom level, so I tried to
multiply it with getScale(). According to getUnits() the result should
bei meters - but the numbers seem to be random.

 

I'm a little desperate - help is very much appreciated.

Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20100625/374bd7c0/attachment.html


More information about the Users mailing list