The PHP website created by its users!
Home
»
PHP » zip radius search
|
zip radius search
| Author | andreas beder |
| Description | its a simple zip radius search. you give a zip and a distance and get an array of zips near your given zip. based on opengeodb |
| Rating | (3 votes) |
PHP Code
<?php
// at first the mysql connection
$connect=@mysql_connect("localhost", "user", "pass")
or die("Cant connect to Database");
@mysql_select_db("geo_plz", $connect)or die("Cant select Database");
// the zip
$plz = '9220';
// the radius in km
$umkreis = 5;
// Earth radius in km
$radius = 6368;
/* -------------------------- */
$sql_rad = mysql_query("SELECT lon, lat FROM `plz_at` WHERE `plz` = '$plz' ");
$erg_rad = mysql_fetch_object($sql_rad);
// Convertation from GRAD to RAD
$lon = $erg_rad->lon / 180 * M_PI;
$lat = $erg_rad->lat / 180 * M_PI;
// now the basic query
$query = "SELECT ort, plz, (
".$radius." * SQRT(2*(1-cos(RADIANS(lat)) *
cos(".$lat.") * (sin(RADIANS(lon)) *
sin(".$lon.") + cos(RADIANS(lon)) *
cos(".$lon.")) - sin(RADIANS(lat)) * sin(".$lat.")))) AS Distance
FROM plz_at WHERE
".$radius." * SQRT(2*(1-cos(RADIANS(lat)) *
cos(".$lat.") * (sin(RADIANS(lon)) *
sin(".$lon.") + cos(RADIANS(lon)) *
cos(".$lon.")) - sin(RADIANS(lat)) * sin(".$lat."))) <= ".$umkreis."
ORDER BY Distance
";
// the output
$sql = mysql_query($query);
while( $erg = mysql_fetch_object($sql) ) {
echo '
<pre>', print_r($erg), '</pre>
';
}
?>
Comments
No comments posted yet.
|