middle of a line defined by two coordinates

2,186 views
Skip to first unread message

bertaudmarc

unread,
May 20, 2011, 6:48:46 AM5/20/11
to Google Maps JavaScript API v3
Let a line defined by two points (Lat/Lng)
Does exist a function in a library to determine the middle point of
the line ?

Rossko

unread,
May 20, 2011, 9:18:03 AM5/20/11
to Google Maps JavaScript API v3
> Does exist a function in a library to determine the middle point of
> the line ?
First you'll need to work out what you mean by middle.
If your line is long enough, the actual midway point between A and B
may not be on the line at all, see 'geodesic'.
If you calculate the middle distance, it may not look like the middle
on screen due to the distortion of map projection.
The easy way to do that I think is to find the length of the line,
half it, and plot your point at half-distance along the line. There
are tools for that.

bertaudmarc

unread,
May 20, 2011, 9:44:13 AM5/20/11
to Google Maps JavaScript API v3
You are right: I should write "middle of the segment" or half route
or ...
1) l = computeDistanceBetween(point1,point2)
2) l = l / 2
3) position = ?
4) set marker

bertaudmarc

unread,
May 20, 2011, 10:34:37 AM5/20/11
to Google Maps JavaScript API v3
I don't know if this approach is correct ...

function middle (P1,P2) {
var lat = (P1.lat()+P2.lat())/2;
var lng = P1.lng()-P2.lng();
if (lng <= 180 && lng >= -180) {
lng = (P1.lng()+P2.lng())/2;
} else {
lng = (P1.lng()+P2.lng()+360)/2;
}
return (lat, lng)

Rossko

unread,
May 20, 2011, 10:44:39 AM5/20/11
to Google Maps JavaScript API v3
> I don't know if this approach is correct ...

No, but it might be good enough for lines of a km or two

This should help with get-point-at-distance-along-line
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/abad905b34b258d9/af74431e96740180

Length of line you already asked about
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/c189ecf6734b5062/0804f85f26c0b50c

Andrew Leach

unread,
May 20, 2011, 10:43:58 AM5/20/11
to google-map...@googlegroups.com
On 20 May 2011 15:34, bertaudmarc <berta...@gmail.com> wrote:
> I don't know if this approach is correct ...

That would work for Euclidean (plane) geometry. It won't work well for
spherical geometry. Have you actually tried plotting it? How about a
line between Trondheim Norway and Bangkok?

Rossko has already pointed out what you need to consider. Is your half-way point
* half-way along a direct (great-circle) line from A to B?
* half-way along a line drawn on a projected map?

For the first scenario, see this page:
http://www.movable-type.co.uk/scripts/latlong.html

For the second scenario, assuming it's a straight line and not a
geodesic line, you need to convert A and B to pixel coordinates, find
the mid-point pixel coordinate and then find the lat-lng coordinates
of that point.

bertaudmarc

unread,
May 20, 2011, 1:34:30 PM5/20/11
to Google Maps JavaScript API v3
I wrote "middle of segment" ... then "This is the half-way point along
a great circle path between the two points" as your reference writes.
Very good reference. Thank you very much !

On 20 mai, 16:43, Andrew Leach <andrew.leac...@gmail.com> wrote:

JKurtock

unread,
May 21, 2011, 11:57:08 AM5/21/11
to Google Maps JavaScript API v3

Understanding the math is important, but i doubt you could ever
improve on the google maps geometry library. Check out the
computeOffset function.

Jeff

geoco...@gmail.com

unread,
May 21, 2011, 1:09:18 PM5/21/11
to Google Maps JavaScript API v3
On May 20, 10:34 am, bertaudmarc <bertaudm...@gmail.com> wrote:
> I wrote "middle of segment" ... then "This is the half-way point along
> a great circle path between the two points" as your reference writes.
> Very good reference. Thank you very much !

Is this something like what you are trying to do?
http://www.geocodezip.com/v3_polyline_example_geodesic_proj.html

-- Larry

bertaudmarc

unread,
May 21, 2011, 5:29:45 PM5/21/11
to Google Maps JavaScript API v3
YES: center of a geodesic polyline
Thank you

On 21 mai, 19:09, "geocode...@gmail.com" <geocode...@gmail.com> wrote:
> On May 20, 10:34 am, bertaudmarc <bertaudm...@gmail.com> wrote:
>
> > I wrote "middle of segment" ... then "This is the half-way point along
> > a great circle path between the two points" as your reference writes.
> > Very good reference. Thank you very much !
>
> Is this something like what you are trying to do?http://www.geocodezip.com/v3_polyline_example_geodesic_proj.html
Reply all
Reply to author
Forward
0 new messages