views:

144

answers:

3

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.

+1  A: 

A big topic. Here are some intro links:

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

http://jan.ucc.nau.edu/~cvm/latlongdist.html

Smandoli
I was about to post your first link, too :)
sum1stolemyname
+1  A: 

UPDATE:

PHP trigonometric functions take radians as Paramters, not degrees, so you need to use deg2rad() as a parameter:

sin(deg2rad($lat))

http://www.php.net/manual/en/function.deg2rad.php


Original answer:

A big Topic indeed.

Depending on your required precision ( and distances covered), you might have to take into account that earth is not a perfect sphere, but a geoid ( a flattened elipsoid).

http://en.wikipedia.org/wiki/Earth_radius

will get you started on this.

Mapping and projection are two topics you should take a look at, too

another link from wikipedia on the topic of distances

http://en.wikipedia.org/wiki/Geographical_distance

sum1stolemyname
Where is this planet 'erath' and how did you get here?
Just get another 200 reputation and edit typos if you find them ;)
sum1stolemyname
A: 

Based on your new information, I have two alternative approaches. (1) Google "PHP GIS". You will find some interesting resources. Maybe one will work. (2) If your enterprises are identified by lat-longitude, then you will have to use (1) I think. But is there a "dumber" approach? For example, if each enterprise is linked to a city, then use simple map coordinates ("K16") to identify cities. Or something a bit smarter code-wise, but that's the idea.

Smandoli