A " Zip code radius search allows "the selection of records within any radius around a given zip code.
Many websites use this technique to your users with the opportunity to search results only from your vicinity (eg 50 km) display a value.
are typically used in the store search (Dealer Locator) or dating sites, real estate portals, on / or sale classifieds and auction sites like Ebay.
This requires is a database of all postal codes and the associated geo-coordinates (longitude / latitude).
Furthermore you need a script that performs the necessary calculation and the calculated eligible postcodes.
The distance calculation on a spherical surface (ground) between two points is called Orthodromie and is given by:
Distance (A, B) = 6371 * ACos (cos (lata) * cos (LatB) * Cos (LngB - Inga) + sin (lata) * Sin (LatB))
The result is the distance between point A and point B in kilometers.
see Below A form based on PHP and MySQL script that implements this calculation:
/ / Form
\u0026lt;form action = "\u0026lt;php echo $ _SERVER ['PHP_SELF'],>?" Method = "get">
\u0026lt;input type = "text" name = "postcode" value = "\u0026lt;php echo $ _GET ['postcode'];?>" maxlength = "5" /> \u0026lt;br />
\u0026lt; ; input type = "text" name = "distance" value = "\u0026lt;php echo $ _GET ['distance'];?>" maxlength = "5" /> \u0026lt;br />
\u0026lt;input type = "submit" name = "search" value = "Search" />
</form>
// PHP-Code
<?php
if(isset($_GET['Search']))
{
$zipcode = $_GET['postcode'];
$distance = $_GET['distance'];
$conn = mysql_connect('127.0.0.1', 'root', '') or die('db connect error: ' . mysql_error());
mysql_select_db('igonow', $conn) or die('could not select database');
$sqlstring = "SELECT * FROM geodb WHERE postcode = '".$zipcode."'";
$result = mysql_query($sqlstring);
$row = mysql_fetch_assoc($result);
$lng = $row["longitude"] / 180 * M_PI;
$lat = $row["latitude"] / 180 * M_PI;
mysql_free_result($result);
$sqlstring2 = "SELECT DISTINCT geodb.postcode,geodb.place,(6367.41*SQRT(2*(1-cos(RADIANS(geodb.latitude))*cos(".$lat.")*(sin(RADIANS(geodb.longitude))*sin(".$lng.")+cos(RADIANS(geodb.longitude))*cos(".$lng."))-sin(RADIANS(geodb.latitude))* sin(".$lat.")))) AS Distance FROM geodb AS geodb WHERE (6367.41*SQRT(2*(1-cos(RADIANS(geodb.latitude))*cos(".$lat.")*(sin(RADIANS(geodb.longitude))*sin(".$lng.")+cos(RADIANS(geodb.longitude))*cos(".$lng."))-sin(RADIANS(geodb.latitude))*sin(".$lat."))) \u0026lt;= '". $ Distance."') ORDER BY distance ";
$ Result = mysql_query ($ sqlstring2) or die ('Failed query:'. Mysql_error ());
$ str =" \u0026lt;table width = \\ "300 \\" border = \\ "0 \\" cellspacing = \\ "0 \\" cellpadding = \\ "0 \\"> ";
$ str .=" \u0026lt;tr> "
$ str .=" \u0026lt;th> postcode \u0026lt;/ th> ";
$ str .=" \u0026lt;th> Town \u0026lt;/ th> ";
$ str .=" \u0026lt;th> Distance \u0026lt;/ th> ";
$ str .= "\u0026lt;/ tr>";
while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC)) {
$ str .= "\u0026lt;td>. $ row ['postcode "]."\u0026lt;/ ToString .. $ row ['place "]."\u0026lt;/ ToString round ($ row [' Distance'])." km \u0026lt;/ td> \u0026lt;/ tr> ";}
$ str .= "\u0026lt;/ table>";
mysql_free_result ($ result);
mysql_close ($ conn);
echo $ str;?
}
>
The script mentioned in the table, GeoDB 'contains all postal codes from the area in question (eg Germany, Austria, Switzerland) with the following minimum Datendeldern:
post code, town, longitude latitude. Ie Remarks, longitude, latitude
there for this sort of postcode databases is more than one provider.
Good luck!
0 comments:
Post a Comment