1

been reading up on this in stack overflow and other places, and tried to put together some coding to do the job I need. I will have a zip code (for test purposes it is fixed at 28752) and I want the page to give me locations within 50 miles of the zip code. Most of what I read is way over my head, but I cobbled together the following, and to the best of my limited knowledge it should work (giving me a list of records within the parameters), but it isn't.

First of all I set up the parameters using the following...

$zipcode="28752";
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
$details=file_get_contents($url);
$result = json_decode($details,true);

$lat=$result['results'][0]['geometry']['location']['lat'];

$lng=$result['results'][0]['geometry']['location']['lng'];

$range = 50;  

       // Find Max - Min Lat / Long for Radius and zero point and query  
       $lat_range = $range/69.172;  
       $lon_range = abs($range/(cos($latitude) * 69.172));  
       $min_lat = number_format($lat - $lat_range, "4", ".", "");  
       $max_lat = number_format($lat + $lat_range, "4", ".", "");  
       $min_lon = number_format($lng - $lon_range, "4", ".", "");  
       $max_lon = number_format($lng + $lon_range, "4", ".", "");  

Then I set up the sql query as follows...

$LA1_RESULTS = "-1";
if (isset($min_lat)) {
$LA1_RESULTS = $min_lat;
}
$LO1_RESULTS = "-1";
if (isset($min_long)) {
$LO1_RESULTS = $min_long; 
}
$LO2_RESULTS = "-1";
if (isset($max_long)) {
$LO2_RESULTS = $max_long;
}
$LA2_RESULTS = "-1";
if (isset($max_lat)) {
$LA2_RESULTS = $max_lat;
}
mysql_select_db($database_XMASTREE, $XMASTREE);
$query_RESULTS = sprintf("SELECT * FROM zip_code WHERE zip_code.latitude BETWEEN %s AND      %s AND zip_code.longitude BETWEEN %s AND %s", GetSQLValueString($LA1_RESULTS, "int"),GetSQLValueString($LA2_RESULTS, "int"),GetSQLValueString($LO1_RESULTS, "int"),GetSQLValueString($LO2_RESULTS, "int"));
$RESULTS = mysql_query($query_RESULTS, $XMASTREE) or die(mysql_error());
$row_RESULTS = mysql_fetch_assoc($RESULTS);
$totalRows_RESULTS = mysql_num_rows($RESULTS);

The output can be seen at http://signature-online.net/xmastrees/zipsearch.php where it gives the lat and long variables correctly. I have set up the lat and long in the database as decimal (6 decimal places).

I am the original "know just enough to be dangerous"! Can anyone figure out what I have done wrong?

Bill Teale
  • 159
  • 3
  • 15

2 Answers2

1

Do you realize this method will return points in a square with sides of 50, not a circle with radius 50?

The basic math is covered in Equation for testing if a point is inside a circle.

So instead of figuring out your max/min for your geo points, you can let the server most of the work:

SELECT * 
FROM zip_code 
WHERE 
    (
        POWER(latitude - "INPUT LATITUDE", 2) 
        + 
        POWER(longitude - "INPUT LONGITUDE", 2) 
    ) 
    < POWER("RADIUS",2)
Community
  • 1
  • 1
K.A.F.
  • 2,277
  • 1
  • 16
  • 17
  • Thank you to all of you who responded. It was way over my head to get this working as sophisticated as your solution looks, it kept telling me there was a coding error. So because this is a very small databse (only 30 records) I actually looked up the zip codes within the radius and added them as a list in a separate field. Then I was able to create the search paramaters within my limited knowledge. I really appreciate the answers you guys gave! My non-techie workaround will probably have you chuckling! – Bill Teale Feb 08 '14 at 02:17
  • well I added the query using DW CS6 and it tells me there is a coding error in $query_ZIPLOCATE = "SELECT * FROM MEMBERS WHERE ( POWER(latitude - "INPUT LATITUDE", 2) + POWER(longitude - "INPUT LONGITUDE", 2) ) < POWER("RADIUS",2)"; – Bill Teale Feb 09 '14 at 13:36
  • The parts in double quotes are meant to be replaced your variable numbers, by the lat and long of your center point (ie `$lat` and `$lng`) and the radius of your search (ie `$range/69.172`) – K.A.F. Feb 10 '14 at 22:45
0

Try setting decimal to 8 places.

Sirago
  • 101
  • 3