Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add kilometers to a map point

Good morning.

I would like to know how do I add kilometers to a map point (latitude / longitude).

For example: The city Jaraguá do Sul is in latitude -26.462049, longitude -49.059448. I want to add 100 kilometers up, down, and on the sides. I want to do a square and get the new points.

How do I do that?

I tried it:

<?php
$distance = 100;
$earthRadius = 6371;
$lat1 = -26.4853239150483;
$lon1 = -49.075927734375;
$bearing = 0;

$lat2 = asin(sin($lat1) * cos($distance / $earthRadius) + cos($lat1) * sin($distance / $earthRadius) * cos($bearing));
$lon2 = $lon1 + atan2(sin($bearing) * sin($distance / $earthRadius) * cos($lat1), cos($distance / $earthRadius) - sin($lat1) * sin($lat2));

echo 'LAT: ' . $lat2 . '<br >';
echo 'LNG: ' . $lon2;
?>

But it's returning wrong cordinates. Thank you!

Thank you very much.

like image 318
proveyourselfthom Avatar asked Apr 19 '10 14:04

proveyourselfthom


People also ask

Can I create a radius in Google Maps?

Google Maps does not support the radius functionality, which means that you can't determine the radius around a given location. But you can measure the distance between two or more points.

Can you Measure between two points on Google Maps?

When using Google Maps in a desktop web browser, right-click the city or starting point you want to use and select “Measure distance” from the menu. Next, click the second point on the map to see the direct distance in miles and kilometers displayed in a small box at the bottom of the window.


1 Answers

as was already pointed out. PHP trigonometric functions take radians as paramters.

degree to radian conversions of the parameters will do the trick. you probably want the result in degrees, so use rad2deg to convert back:

<?php
$distance = 100;
$earthRadius = 6371;
$lat1 = deg2rad(-26.4853239150483);
$lon1 = deg2rad(-49.075927734375);
$bearing = deg2rad(0);

$lat2 = asin(sin($lat1) * cos($distance / $earthRadius) + cos($lat1) * sin($distance / $earthRadius) * cos($bearing));
$lon2 = $lon1 + atan2(sin($bearing) * sin($distance / $earthRadius) * cos($lat1), cos($distance / $earthRadius) - sin($lat1) * sin($lat2));

echo 'LAT: ' . rad2deg($lat2) . '<br >';
echo 'LNG: ' . rad2deg($lon2);
?>
like image 145
thrau Avatar answered Oct 18 '22 03:10

thrau