Saturday, January 29, 2011

calculate distance using latitude longitude


Simple php-mysql query to calculate nearby locations

$sql=mysql_query (“SELECT distinct name, ( 3959 * acos( cos( radians( ‘$lat’ ) ) * cos( radians( lat ) ) * cos( radians( lng ) – radians( ‘$long’ ) ) + sin( radians( ‘$lat’ ) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < ’200′ “);
 

where markers is your table, $lat and $long are point given by you, lat lng are your fields in markers table. Above query will result points upto 200 miles distance.

No comments:

Post a Comment