Jump to content

Get all records where zip is within distance of another zip


sanfordss

Recommended Posts

I have this script that allows me to display all zipcodes in a specified radius to another zip. What I need is to take that information and display all records from a table where the Zip is one inside that radius. Can someone show me the way? Thanks!

 

<?php

 

require_once('zipcode.class.php');      // zip code class

 

mysql_connect('localhost','root','') or die(mysql_error());

mysql_select_db('members') or die(mysql_error());

 

$z = new zipcode_class;

 

 

// Below is an example of how to return an array with all the zip codes within

// a range of a given zip code along with how far away they are.  The array's

// keys are assigned to the zip code and their value is the distance from the

// given zip code. 

 

echo '<h3>A sample getting all the zip codes withing a range: 2 miles from 97214</h3>';

 

$zips = $z->get_zips_in_range('97214', 2, _ZIPS_SORT_BY_DISTANCE_ASC, true);

 

if ($zips === false) echo 'Error: '.$z->last_error;

else {

 

  foreach ($zips as $key => $value) {

      echo "Zip code <b>$key</b> is <b>$value</b> miles away from <b>97214</b>.<br />";

  }

 

 

  }

 

 

?>

 

Just put all the returned zip codes in a query and return those rows..

 

$query = "SELECT whatever_data FROM your_table WHERE zipcode IN ( '" . implode ( "', '", $zips ) . "' );";

 

Bust I wouldn't use that zip class you're using because it returns bogus results. Anything more than 1/2 mile radius and that class will not return all the results that are really in that radius. Example using 2 miles from 97214 should return 25 different zip codes....

 

*   	OR  	Portland  	529,121  	97214  	503  	Pacific  	drive distance  straight line distance
01 	OR 	Portland 	529,121 	97293 	503 	Pacific 	0.39 miles 	0.34 miles
02 	OR 	Portland 	529,121 	97232 	503 	Pacific 	1.29 miles 	1.12 miles
03 	OR 	Portland 	529,121 	97240 	503 	Pacific 	1.35 miles 	1.17 miles
04 	OR 	Portland 	529,121 	97204 	503 	Pacific 	1.67 miles 	1.45 miles
05 	OR 	Portland 	529,121 	97251 	503 	Pacific 	1.68 miles 	1.46 miles
06 	OR 	Portland 	529,121 	97255 	503 	Pacific 	1.78 miles 	1.55 miles
07 	OR 	Portland 	529,121 	97258 	503 	Pacific 	1.81 miles 	1.58 miles
08 	OR 	Portland 	529,121 	97299 	503 	Pacific 	1.84 miles 	1.60 miles
09 	OR 	Portland 	529,121 	97283 	503 	Pacific 	1.85 miles 	1.61 miles
10 	OR 	Portland 	529,121 	97259 	503 	Pacific 	1.93 miles 	1.68 miles
11 	OR 	Portland 	529,121 	97272 	503 	Pacific 	2.03 miles 	1.77 miles
12 	OR 	Portland 	529,121 	97256 	503 	Pacific 	2.15 miles 	1.87 miles
13 	OR 	Portland 	529,121 	97253 	503 	Pacific 	2.17 miles 	1.89 miles
14 	OR 	Portland 	529,121 	97215 	503 	Pacific 	2.30 miles 	2.01 miles
15 	OR 	Portland 	529,121 	97207 	503 	Pacific 	2.41 miles 	2.10 miles
16 	OR 	Portland 	529,121 	97212 	503 	Pacific 	2.49 miles 	2.17 miles
17 	OR 	Portland 	529,121 	97228 	503 	Pacific 	2.49 miles 	2.17 miles
18 	OR 	Portland 	529,121 	97209 	503 	Pacific 	2.50 miles 	2.18 miles
19 	OR 	Portland 	529,121 	97205 	503 	Pacific 	2.54 miles 	2.21 miles
20 	OR 	Portland 	529,121 	97242 	503 	Pacific 	2.62 miles 	2.28 miles
21  	OR  	Portland  	529,121  	97202  	503  	Pacific  	2.67 miles  	2.33 miles
22 	OR 	Portland 	529,121 	97282 	503 	Pacific 	2.75 miles 	2.39 miles
23 	OR 	Portland 	529,121 	97239 	503 	Pacific 	2.90 miles 	2.53 miles
24 	OR 	Portland 	529,121 	97213 	503 	Pacific 	2.94 miles 	2.56 miles
25 	OR 	Portland 	529,121 	97227 	503 	Pacific 	2.96 miles 	2.58 miles

 

Does your class return 25 zip codes? If it doesn't, it not doing an accurate radius calculation. If your not going to us geo spiral data, then you're best using Vance Brand's formula, it's very accurate because it uses geo plates to measure distances moving across the plain of travel, just like gps navagation does.

Hmm, you're right. It only shows 15 zips. This looked simple enough when I put it together, but I don't want to leave out a third of the zips. Is there a class somewhere that easily allows me to use one of the other methods you listed? I tried to look up Vance Brand, but didn't get much.

If your class returns to many zip codes, then you can slice the return array...

 

Example...

 


// whatever ...

$zips = $z->get_zips_in_range('97214', 2, _ZIPS_SORT_BY_DISTANCE_ASC, true); 

if ($zips === false)
{
echo 'Error: ' . $z->last_error;
}
else
{
// maximum zip codes to query against

$max = 5;

if ( sizeof ( $zips ) > $max ) )
{
	$zips = array_slice ( $zips, 0, 5 );
}

$query = "SELECT whatever_data FROM your_table WHERE zipcode IN ( '" . implode ( "', '", $zips ) . "' );";

// run result loop here..
}

 

 

 

I have the zip code class and the pre-compiled Vance Brand database (Jan 28, 2009) at work, tomorrow morning I will post a link to it, (zip file) and a good example that will show you how to use it... The data comes from the US Postal Service, so it up to date...

 

Jan 28, 2009 8:44 AM EST, USPS NORTHERN DISTRICT BOSTON MA 02112

POSTMASTER GENERAL NOTICE: John E. Potter

ZIP CODE UPDATE VALID THROUGH Apr 30, 2009, 12:00 AM EST

COLUMN DATA TYPES

/* example data */

1','NY','1','New York','Holtsville','00501','N','40.813061','W','-73.046371','Suffolk','C','631','Eastern','1','-5','17006','25','36','103

COLUMN DATA TYPES

column_number            = 1
state_abbreviated        = NY
geo_position             = 1 Vance Brand (geo plate)
state_full               = New York
city_name                = Holtsville
zip_code                 = 00501
lat_common_name          = N
lat_position             = 40.813061 (zip code center point)
lon_common_name          = W
lon_position             = -73.046371 (zip code center point)
county_name              = Suffolk
zip_code_type            = C
area_code                = 631
common_time_zone_name    = Eastern
flag_uses_DST            = 1
time_zone_from_GMT       = -5
city_population          = 17006
elevation_at_lat_and_lon = 25
state_fips_code          = 36
county_fips_code         = 103


NOTES:

columns split by         => ','

columns escaped by       => \
  
lines terminated by      => \r\n

geo_position             => see usps_mapping_formats_nov_2008.pdf (Postal Explorer)

combine                  => state_fips_code, county_fips_code = federal_fips => 36103

flag_uses_DST            => 0 = does not observe DST, 1 = observes DST

zip_code_type            => D = Delivery, P = Post Office Box, C = Company Post Office Box

elevation_at_lat_and_lon => value in (feet)

+//+

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.