Google-maps – How to search for all points in a given radius of a zipcode with google maps api

google mapsgoogle-maps-api-3

Is there a way with Google Maps API, that I can find all red balloon points (addresses pinned on my google map) within a given radius (in miles) of a certain zip code?

For example…

  • The first input box asks you for your zip code. Type it in.

  • The second input box (a section box with < options >) asks you for
    the radius in miles, 10 miles, 20 miles, 30 miles..etc

  • Then you click submit

  • Then the google map shows all the addresses (which were stored from a
    json file), on to the google map in red balloons.

Is there free learning material on the web that will help me to build something like this?

I've been reading google maps api stuff all morning and haven't found any examples that can offer this.

Thanks!

Best Answer

Here are the two high-level steps you need to follow to implement this:

1) You want to convert that zipcode (or an address in many different forms!) to a latitude and longitude coordinate using the Google Geocoder API.

2) Dynamically create a GET request to a script that queries your database based on the distance the user entered (5 miles, 10 miles, etc.) like so:

var searchUrl = 'phpsqlajax_search.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;

Look at my answer to this related question so you can take user input and geocode the address: Store locatore, how to use/input my postion?

The Google Maps API provides extensive documentation on finding the closest items in a database to a certain latitude and longitude coordinate. It can be found here: https://developers.google.com/maps/articles/phpsqlsearch_v3

Note: The documentation uses PHP, Javascript, and MySQL.