Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a marker 100 meters with coordinates

I have 2 coordinates. Coordinate 1 is a 'person'. Coordinate 2 is a destination.

How do I move coordinate 1 100 meters closer to coordinate 2?

This would be used in a cron job, so only php and mysql included.

For example:

Person is at: 51.26667, 3.45417

Destination is: 51.575001, 4.83889

How would i calculate the new coordinates for Person to be 100 meters closer?

like image 548
Rene Pot Avatar asked Jun 18 '10 21:06

Rene Pot


1 Answers

Use Haversine to calculate the difference between the two points in metres; then adjust the value of the person coordinates proportionally.

$radius = 6378100; // radius of earth in meters
$latDist = $lat - $lat2;
$lngDist = $lng - $lng2;
$latDistRad = deg2rad($latDist);
$lngDistRad = deg2rad($lngDist);
$sinLatD = sin($latDistRad);
$sinLngD = sin($lngDistRad);
$cosLat1 = cos(deg2rad($lat));
$cosLat2 = cos(deg2rad($lat2));
$a = ($sinLatD/2)*($sinLatD/2) + $cosLat1*$cosLat2*($sinLngD/2)*($sinLngD/2);
if($a<0) $a = -1*$a;
$c = 2*atan2(sqrt($a), sqrt(1-$a));
$distance = $radius*$c;

Feeding your values of:

$lat = 51.26667;        //  Just South of Aardenburg in Belgium
$lng = 3.45417;
$lat2 = 51.575001;      //  To the East of Breda in Holland
$lng2 = 4.83889;

gives a result of 102059.82251083 metres, 102.06 kilometers

The ratio to adjust by is 100 / 102059.82251083 = 0.0009798174985988102859004569070625

$newLat = $lat + (($lat2 - $lat) * $ratio);
$newLng = $lng + (($lng2 - $lng) * $ratio);

Gives a new latitude of 51.266972108109 and longitude of 3.4555267728867

like image 192
Mark Baker Avatar answered Nov 01 '22 13:11

Mark Baker