Reputation: 8379
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!
Upvotes: 1
Views: 12553
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
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