how to calculate LatLng with x meters to left/right

1,326 views
Skip to first unread message

serj

unread,
Aug 30, 2010, 8:58:54 AM8/30/10
to Google Maps JavaScript API v3
I want to calculate new location (on same latitude) for a point at a
given distance. Let's say at 100 meters to left from first location.

thanks for any ideea
serj

Ronny Aerts

unread,
Aug 30, 2010, 9:03:06 AM8/30/10
to google-map...@googlegroups.com
Could my code at http://www.grasoft.be/geotools/ help you?

vriendelijke groeten,
Ronny Aerts

geoco...@gmail.com

unread,
Aug 30, 2010, 9:18:26 AM8/30/10
to Google Maps JavaScript API v3
On Aug 30, 5:58 am, serj <fs...@yahoo.com> wrote:
> I want to calculate new location (on same latitude) for a point at a
> given distance. Let's say at 100 meters to left from first location.

http://www.movable-type.co.uk/scripts/latlong.html

See "Destination point given distance and bearing from start point"

-- Larry

serj

unread,
Aug 30, 2010, 9:51:18 AM8/30/10
to Google Maps JavaScript API v3
based on http://www.movable-type.co.uk/scripts/latlong.html,
I have this code, but something is wrong. Final point is not on same
latitude


> http://www.movable-type.co.uk/scripts/latlong.html
>
> See "Destination point given distance and bearing from start point"
>
>   -- Larry


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta http-equiv="pragma" content="no-cache">

<title>Insert title here</title>
<style type="text/css">
#map {
width: 1000px;
height: 600px;
top: 20px;
left: 20px;
}
#latlng-control {
background: #ffc;
border: 1px solid #676767;
position: absolute;
top: 100px;
left: 100px;
width: 400px;
height: 40px;
}

</style>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?
sensor=false"></script>
<script type="text/javascript">
var EarthRadiusMeters = 6371000; // meters

google.maps.LatLng.prototype.DestinationPoint = function (brng, dist)
{
var R = EarthRadiusMeters;
var brng = brng.toRad();
var lat1 = this.lat().toRad(), lon1 = this.lng().toRad();
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist/R) +
Math.cos(lat1)*Math.sin(dist/
R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist/
R)*Math.cos(lat1),
Math.cos(dist/R)-
Math.sin(lat1)*Math.sin(lat2));

return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg());
}

Number.prototype.toRad = function () {
return this * Math.PI / 180;
};


Number.prototype.toDeg = function () {
return this * 180 / Math.PI;
};

var map;
var overlay;
var bounds;
var hundredMeterBounds = new google.maps.LatLngBounds();

function init() {
var centerLatLng = new google.maps.LatLng(45, 0);
map = new google.maps.Map(document.getElementById('map'), {
'zoom' : 8,
'center' : centerLatLng,
'mapTypeId' : google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker( {
position : centerLatLng,
map : map,
title:"Initial point ["+centerLatLng.lat()+", "+centerLatLng.lng()
+"]"
});

var rightPoint = centerLatLng.DestinationPoint(90, 200*1000);//800
km
var marker2 = new google.maps.Marker( {
position : rightPoint,
map : map,
title:"Final point ["+rightPoint.lat()+", "+rightPoint.lng()+"]"
});

drawLine(centerLatLng,rightPoint,"#FF0000");

var elem = document.getElementById('latlng-control');
elem.innerHTML = ("Initial ["+centerLatLng.lat()+",
"+centerLatLng.lng()+"]"+"<br>"+
"Final ["+rightPoint.lat()+", "+rightPoint.lng()+"]");
}

function drawLine(point1,point2,color){
var flightPlanCoordinates = [point1,point2];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: color,
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);
}

google.maps.event.addDomListener(window, 'load', init);
</script>
</head>
<body>
<div id="map"></div>
<div id="latlng-control"></div>
</body>
</html>

geoco...@gmail.com

unread,
Aug 30, 2010, 10:05:46 AM8/30/10
to Google Maps JavaScript API v3
On Aug 30, 6:51 am, serj <fs...@yahoo.com> wrote:
> based on  http://www.movable-type.co.uk/scripts/latlong.html,
> I have this code, but something is wrong. Final point is not on same
> latitude
>
> >http://www.movable-type.co.uk/scripts/latlong.html
>
> > See "Destination point given distance and bearing from start point"
>

Please post a link to your map that shows the problem, not code.

-- Larry

serj

unread,
Aug 30, 2010, 11:27:22 AM8/30/10
to Google Maps JavaScript API v3
my code example is visible at http://fserj.eu5.org/test2.html

geoco...@gmail.com

unread,
Aug 30, 2010, 12:11:11 PM8/30/10
to Google Maps JavaScript API v3
On Aug 30, 8:27 am, serj <fs...@yahoo.com> wrote:
> my code example is visible athttp://fserj.eu5.org/test2.html

If I plug your numbers into the page at:
http://www.movable-type.co.uk/scripts/latlong.html

I get the same result. The options I see (there may be others,
depending on your goal):
1. use the longitude of the result, and force the latitude to be the
same, see if the distance is close enough
2. draw a line along the constant latitude and use the epoly
GetPointAtDistance to truncate the line at the correct distance (it
may take a few iterations to get it right).
3. use a version of GetPointAtDistance that accounts for the
projection

Note that as Chris says in his notes on http://www.movable-type.co.uk/scripts/latlong.html:
> Accuracy: since the earth is not quite a sphere, there are small errors in using spherical geometry; the earth is
> actually roughly ellipsoidal (or more precisely, oblate spheroidal) with a radius varying between about 6,378km
> (equatorial) and 6,357km (polar), and local radius of curvature varying from 6,336km (equatorial meridian) to
> 6,399km (polar). 6,371 km is the generally accepted value for the Earth’s mean radius. This means that errors
> from assuming spherical geometry might be up to 0.55% crossing the equator, though generally below 0.3%,
> depending on latitude and direction of travel.

-- Larry

William

unread,
Aug 31, 2010, 2:31:51 AM8/31/10
to Google Maps JavaScript API v3
On Aug 30, 11:51 pm, serj <fs...@yahoo.com> wrote:
> I have this code, but something is wrong. Final point is not on same
> latitude
>
> >http://www.movable-type.co.uk/scripts/latlong.html
>
> > See "Destination point given distance and bearing from start point"
>

that formula assumes you are travelling on a great circle route,
however lines of latitudes aren't great circles (except for the
equator).

a great circle route between two points of constant latitude will arc
towards the pole and the bearing will vary along the length of the
route.

It might be more suitable for you to use rhumb lines, which are lines
on a sphere that follow a constant bearing.

See the Routeboxer code, which contains a rhumbDestinationPoint
function, with bearing and distance arguments:

google.maps.LatLng.prototype.rhumbDestinationPoint = function (brng,
dist)

http://google-maps-utility-library-v3.googlecode.com/svn/tags/routeboxer/1.0/src/

...

geoco...@gmail.com

unread,
Aug 31, 2010, 3:33:50 AM8/31/10
to Google Maps JavaScript API v3
That certainly seems to solve the issue (makes me yearn for the good
old days when the earth was flat...)

http://www.geocodezip.com/v3_polygon_example_rhumb_squares.html

-- Larry


>
> http://google-maps-utility-library-v3.googlecode.com/svn/tags/routebo...
>
> ...

Bost

unread,
Aug 31, 2010, 5:57:27 AM8/31/10
to Google Maps JavaScript API v3
For the same latitude on the same circle of latitude:
deltaLongitude= distance/(R* Cos(latitude)) in radians

Convert it in degrees by multiplying with 360/2Pi.

with:
R=earth radius=6378.137 km
distance= distance of the 2 points on the circle of latitude in kms

Regards
Bost

serj

unread,
Aug 31, 2010, 6:29:33 AM8/31/10
to Google Maps JavaScript API v3

>
> That certainly seems to solve the issue (makes me yearn for the good
> old days when the earth was flat...)
>
> http://www.geocodezip.com/v3_polygon_example_rhumb_squares.html
>
>   -- Larry
>

Using rhumbDestinationPoint is better. It keeps the same latitude, But
I see an error. You set distance to 800Km, and distance is 799.783 Km.
I think method is ok for smaller distance so error 0.55%-0.3% to be
ok.

serj

William

unread,
Aug 31, 2010, 9:23:50 AM8/31/10
to Google Maps JavaScript API v3
On Aug 31, 8:29 pm, serj <fs...@yahoo.com> wrote:
> I see an error. You set distance to 800Km, and distance is 799.783 Km.
that might be a shortcut, a quicker way between two points, via the
great circle?

...

serj

unread,
Aug 31, 2010, 9:51:16 AM8/31/10
to Google Maps JavaScript API v3
I don't think it is a quicker way

geoco...@gmail.com

unread,
Aug 31, 2010, 10:05:35 AM8/31/10
to Google Maps JavaScript API v3
Quicker isn't the point. You put 2 points on a sphere at a certain
distance following the line of latitude. Then you calculate the
distance between them "as the crow flies" (straight line along the
sphere). That is shorter than the distance along the line of
latitude.

-- Larry

geoco...@gmail.com

unread,
Aug 31, 2010, 10:12:14 AM8/31/10
to Google Maps JavaScript API v3
On Aug 31, 7:05 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
See how the geodesic polyline (red) and the rhumb polyline (blue)
diverge. Because of the projection, the red line "looks" longer, but
if you un-project it or measure it on the surface of the earth, the
red line is shorter:
http://www.geocodezip.com/v3_polygon_example_rhumb_squares.html

  -- Larry

geoco...@gmail.com

unread,
Sep 1, 2010, 7:21:02 PM9/1/10
to Google Maps JavaScript API v3
On Aug 31, 7:12 am, "geocode...@gmail.com" <geocode...@gmail.com>
This makes it even more obvious:
http://www.geocodezip.com/v3_polyline_example_rhumb.html

-- Larry

>
>    -- Larry- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages