Calculate distance between two or more polylines

17 views
Skip to first unread message

Sveglio

unread,
Apr 2, 2008, 5:01:04 AM4/2/08
to Google AJAX API

Somebody help me to give me the Java code for calculate distance
between two or more polylines?

jgeerdes [AJAX APIs "Guru"]

unread,
Apr 2, 2008, 7:13:15 AM4/2/08
to Google AJAX API
The answer to this question is actually a little more difficult than
it would seem because a polyline is a two-dimensional object. In
other words, the distance between the two lines will depend on which
two points along each respective line you want to measure from and
to. For instance, if I have two lines that run parallel to each other
at a staggered interval, if I measure from the far end of one to the
far end of the other, I will end up with a significantly greater
distance than if I measure from the near one of the one to the near
end of the other. That said, here's how you measure distance from one
arbitrary vertex of the first polyline to another arbitrary vertex of
a second polyline (the code can be looped over to measure the distance
to additional polylines.

var
distInMeters=polylineA.getVertex(VERTEXA_INDEX).getDistance(polylineB.getVertex(VERTEXB_INDEX));

Or, in a function format (i.e., for repeated use):

function getDistBetweenPolylines(polylineA,vertexA,polylineB,vertexB){
return
polylineA.getVertex(vertexA).getDistance(polylineB.getVertex(vertexB);
}
var
distInMeters=getDistBetweenPolylines(polylineA,vertexA,polylineB,vertexB);

Please note that this will return the distance in meters. From there,
you will have to divide by the appropriate denominator to get the unit
you want. For instance, to measure in kilometers, you would divide by
1000; in miles, you would divide by 1609.344.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

jgeerdes [AJAX APIs "Guru"]

unread,
Apr 2, 2008, 7:19:05 AM4/2/08
to Google AJAX API
Oh, and alternatively, if you just wanted to get the distance between
the general areas of the (i.e., the center of the bounds in which
they're located), you could simplify the code a bit, but you aren't
necessarily to get a really exact measurement:

function getDistBetweenPolylineCenters(polylineA,polylineB){
return
polylineA.getBounds().getCenter().getDistance(polylineB.getBounds().getCenter());
}
var distInMeters=getDistBetweenPolylineCenters(polylineA,polylineB);

With this code, the warning above could be demonstrated if you have a
polyline that is shaped like the letter J. In this case, the point
from which the distance is measured would not be on the line at all,
but floating somewhere out between the two ends of the J.

Sveglio

unread,
Apr 3, 2008, 3:55:56 AM4/3/08
to Google AJAX API

Thank you Jeremy, I really appreciate your help.

Just a question:

In this code, how can I make the interchange the values for
polyline(A,B) and vertex(A,B)?
Thank you,
Aldo

jgeerdes [AJAX APIs "Guru"]

unread,
Apr 3, 2008, 7:03:30 AM4/3/08
to Google AJAX API
What do you mean by interchange?
Reply all
Reply to author
Forward
0 new messages