Close up longitude/latitude calculations

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Close up longitude/latitude calculations

Post by Todd_Z »

I am writing a script that has four input variables:

longitude & latitude
delta X and delta Y [both in meters]

I have a function that will evaluate the new longitude and latitude if the distances were miles, but in this case, I am dealing with meter distances of approximately 100 meters, and I feel like the error bounds are two great using the formula designed for miles. Has anyone written a function that deals with this? Or is there a simple conversion from degrees long to meters and degrees lat to meters?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do you have the formula for miles handy (with units)? Conversion to meters isn't too far from it, so it's likely a few constants changes and you're gold.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Code: Select all

function distance ( $lat1, $lon1, $lat2, $lon2 ) {
                $theta = $lon1 - $lon2;
                $dist = sin( deg2rad($lat1) ) * sin( deg2rad($lat2) ) + cos( deg2rad($lat1) ) * cos( deg2rad($lat2) ) * cos( deg2rad($theta) );
                $dist = acos( $dist );
                $dist = rad2deg( $dist );
                $miles = $dist * 60 * 1.1515;
                return $miles;
        }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Since there are no units where it matters, I can only suggest the slightly dirty approach:

Code: Select all

$meters = bcmul( bcmul($dist, '69.09', 20), '1609.344' /* meters per mile */, 20);
return $meters;
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Dirty isn't an option, this has to be as error-less as I can possibly get.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's as close as I can get you without knowing the exact units for the literals.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

I was trying to do something like this when I was using the google maps API, I didn't ever find a way to find the EXACT calculation if you do, please post it up. :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Nearest I can think of is to use the Great Circle distance in kilometers and then the meters to avoid milage conversion. This doesn't take into account the elavation distance. The main trouble you have is that the earth is not round. I know there are quite a few specalist GEO forums out there and I would recommend you check if there is one local to the area you are concerned with. Also note that the information on Long/Lat can also be out depending on the model it uses (WGS84,ED50,NAD,TD) compared with the location you need.
Post Reply