klewis
klewis

Reputation: 8379

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

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...

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!

Upvotes: 1

Views: 12553

Answers (2)

Cybercartel
Cybercartel

Reputation: 12592

When you just want an approximation and a fast result you can also try a space filling curve, i.e. a spatial index. Look for example the internet map from xkcd comic. It uses a hilbert curve to print a map from the internet. Then you can use the curve to transform the geo locations and relax the nearest-neighbor search. The same curve is used in many database engines with the point datatype and a spatial index.

Here you have a javascript hilbert curve: https://github.com/chrisdew/jshilbert. Here a blog about spatial index: http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves. I have wrote a php class hilbert-curve @ phpclasses.org. It also uses a mercator projection.

Upvotes: 2

Matt Kneiser
Matt Kneiser

Reputation: 2156

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.

Upvotes: 2

Related Questions