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.
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.
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.
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);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With