If you are looking for a php seach script to find the items ,profile or anything within a defined radius ,then you have to use geometric formula. In this script we will use php , mysql and google geocoder.
My client need this script for its joomla jomsocial members so that any member can search in radius another member.Using this example of profile search within radius I create this article. So I developed a component for him.You can download the script .
For creating such kind of script you don’t need geocode database or international zip code database, google map api is sufficient for this. You can see its demonstration .
To search data within mysql table we need some raw data present in tables.
Table must contain two essential fields latitude and longitude.
The whole idea is based on latitude and longitude of the address.
What we Need
- Google Geocoder.
- php programming.
- knowledge javascript or jQuery.
Idea behind search within a given distance
Step 1. User address passed in geocoder .
Step 2. Geocoder give us its coordinated ie latitude and longitude.
Step 3. Using some php function we will calculate its min and max distance within defined radius.
Step 4. Using sql query we get its matching profile.
Actually its all depend upon the geographic coordinate i.e. latitude and longitude . We fetch the latitude and longitude from the address of the user with the help of its address. zip code is just for correctness of the address.
This is the post about how to get latitude and longitude from address.
with the help google map api we fetch its geographic coordinate and save them
during search we fetch its latitude and longitude again from the address
and using the following script we can look up the profile in given radius distance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$d = “DISTANCE”; $r = 3959; //earth’s radius in miles $latN = rad2deg(asin(sin(deg2rad($latitude)) * cos($d / $r) + cos(deg2rad($latitude)) * sin($d / $r) * cos(deg2rad(0)))); $latS = rad2deg(asin(sin(deg2rad($latitude)) * cos($d / $r) + cos(deg2rad($latitude)) * sin($d / $r) * cos(deg2rad(180)))); $lonE = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($latitude)), cos($d / $r) – sin(deg2rad($latitude)) * sin(deg2rad($latN)))); $lonW = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($latitude)), cos($d / $r) – sin(deg2rad($latitude)) * sin(deg2rad($latN)))); |
This algorithm compute max and min latitudes / longitudes for search square .
then using following sql query we get the relative results.
Important Code
SQL DATABASE STRUCTURE
in mysql we will create zip code latitude longitude database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
CREATE TABLE IF NOT EXISTS `radius_script` ( `id` int(15) NOT NULL AUTO_INCREMENT, `first_name` varchar(10) NOT NULL, `last_name` varchar(10) NOT NULL, `city` varchar(10) NOT NULL, `state` varchar(50) NOT NULL, `pincode` varchar(10) NOT NULL, `country` varchar(10) NOT NULL, `latitude` varchar(10) NOT NULL, `longitude` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; |
SQL Query For search by zip code
1 2 3 |
SELECT * FROM `radius_script` WHERE (latitude < = $latN AND latitude >= $latS AND longitude < = $lonE AND longitude >= $lonW) AND (latitude != $lat1 AND longitude != $lon1) |
PHP code to compute max and min latitudes / longitudes in search square
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$d = “DISTANCE”; $r = 3959; //earth’s radius in miles $latN = rad2deg(asin(sin(deg2rad($latitude)) * cos($d / $r) + cos(deg2rad($latitude)) * sin($d / $r) * cos(deg2rad(0)))); $latS = rad2deg(asin(sin(deg2rad($latitude)) * cos($d / $r) + cos(deg2rad($latitude)) * sin($d / $r) * cos(deg2rad(180)))); $lonE = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($latitude)), cos($d / $r) – sin(deg2rad($latitude)) * sin(deg2rad($latN)))); $lonW = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($latitude)), cos($d / $r) – sin(deg2rad($latitude)) * sin(deg2rad($latN)))); |
Jquery code for getting latitude and longitude
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
geocoder = new google.maps.Geocoder(); // address variable contains your zip code or area code, city, state etc geocoder.geocode( { ‘address’: address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { jQuery(‘#location-latitude’) .val(results[0].geometry.location.lat()); jQuery(‘#location-longitude’) .val(results[0].geometry.location.lng()); } }); |
PHP Code for getting latitude and longitude
1 2 3 4 5 6 7 8 |
$url = “http://maps.googleapis.com/maps/api/geocode/json?address=”.$string.”&sensor=false”; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = json_decode(curl_exec($ch), true); |
How It Works
During registration.
1. While registration your address city and Zip code / area code pass in Google geocoder .
2. We will get latitude and longitude.
3. And then we will save the longitude and latitude in database.
During Search
1. We will enter the zip code or address.
2. This address again goes to Google geocoder.
3. Again we will get latitude and longitude.
4. Enter the radius in which you want to search the profile. 10 mile, 5 mile etc.
Pictorial Representation
Live Demo | Download |
This is all about search within radius php code with php radius script. hope you like it…