Calculating the radius of a coordinate using PHP

Posted on 01/05/10, in PHP, by kevin

Sometimes you’ll want to get the radius of a location. For this I have written a function that I’ll share with you.

/*

Copyright 2009  Kevin Bradwick (http://www.kevinbradwick.co.uk) 

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. 

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

function geoRadius($lat, $lng, $rad, $kilometeres = FALSE, $out = 'OBJ')
	{
		$radius = ($kilometers) ? ($rad * 0.621371192) : $rad;

		(float)$dpmLAT = 1 / 69.1703234283616; 

		// Latitude calculation
		(float)$usrRLAT = $dpmLAT * $radius;
		(float)$latMIN = $lat - $usrRLAT;
		(float)$latMAX = $lat + $usrRLAT;

		// Longitude calculation
		(float)$mpdLON = 69.1703234283616 * cos($lat * (pi/180));
		(float)$dpmLON = 1 / $mpdLON; // degrees per mile longintude
		$usrRLON = $dpmLON * $radius;
		$lonMIN = $lng - $usrRLON;
		$lonMAX = $lng + $usrRLON;

		$output = array("lonMIN" => $lonMIN, "lonMAX" => $lonMAX, "latMIN" => $latMIN, "latMAX" => $latMAX);
		return ($out == 'OBJ') ? (object)$output : $output;
	}

Okay, so how do you use it? Well, the first two paramaters are the Latitude and Longitude of your origin. The third is the radius ammount. If you want to calculate it based on kilometers (default is in miles), enter a boolean value of TRUE for the fourth parameter. The last paramter specifices the output method, by default it will return an object, enter anything other that OBJ here will return an associative array.

So, once we’ve done that what do we have? You will then have 4 variables to use, each of them minimum and maximum values of Latitude and Longitude. This is how you could use the output of this function to query a database that contains geo data.

$radius = geoRadius(53.741690, -2.397750, 48.28032);
$sql = "SELECT * FROM `mytable` WHERE `latitude` BETWEEN {$radius->latMIN} AND {$radius->latMAX} AND `longitude` BETWEEN {$radius->lonMIN} AND {$radius->lonMAX}";
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogosphere News
  • DZone
  • email
  • LinkedIn
  • MySpace
  • PDF
  • RSS
  • StumbleUpon
  • Twitter

7 Responses to “Calculating the radius of a coordinate using PHP”


Rich
3-29-2010

Hi, I just wondered if you still had your step by step guide available for the PHP radius of a coordinate. I noticed you had it previously and intended to have a read through it.

Cheers
Rich


kevin
3-30-2010

@Rich,

Nope, I condensed it down to this page as there was a lot of useless info in the 3 pages I had originally written. The math invovled can be taken from the function I’ve written. To use this function you’ll need a database that contains all the post codes including their corresponding longitude and latitude values.


Rich
4-1-2010

Nevermind, thanks anyway, I’ll see if I can make sense of it.


Fredrick
7-5-2010

I just like to thank you for this article, it made my day!


Mark
7-5-2010

Am I right in thinking that this gives the lat/lng of two points then – one northeast and one southwest of a point?


kevin
7-5-2010

A radius of a lat/lng point equates to a range, so this function will give you a range of lat/lng points to query a database with.


Mark
7-6-2010

Ah ok, thanks Kevin. Actually I’m trying to write something using the foursquare API, and that only gives you output for a given lat/lng point – it doesn’t have a function where you can pass it a range of lat/lng values. So what I’m trying to do is figure out how to get the lat/lng values of 8 points around a centre – N, NE, E, SE, S, SW, W, NW – and plus those into the API instead…

Leave a Reply