Php search script within a radius


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 .
search by zip code or area code latitude longitude
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 .

Demo

To search data within mysql table we need some raw data present in tables.
Table must contain two essential fields latitude and longitude.
img1
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.
search within-radius

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.

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

SQL Query For search by zip code

PHP code to compute max and min latitudes / longitudes in search square

Jquery code for getting latitude and longitude

PHP Code for getting latitude and longitude

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

search by zip code

Live Demo Download

This is all about search within radius php code with php radius script. hope you like it…

, ,