3

I have a db with clients zip codes, I want to create a page where the user input a zip code and select for example 50 miles and press search, then the page should show all other clients which have zip codes withing the range of this 50 miles.
Means near the given zip code within 50 miles radius.
Is there an API, Lib or something can give me this feature?

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
  • Beside other solutions, some integration with google geocoding api from user position may solve the problem. Some reference here: http://stackoverflow.com/questions/4749706/lookup-city-and-state-by-zip-google-geocode-api – FeliceM Oct 18 '13 at 18:40
  • possible duplicate of [Given coordinates, how do I get all the Zip Codes within a 10 mile radius?](http://stackoverflow.com/questions/4190154/given-coordinates-how-do-i-get-all-the-zip-codes-within-a-10-mile-radius) – Alexei Levenkov Oct 18 '13 at 18:42
  • 1
    What is your definition of distance between zip codes? Distance between the centroids of their defined areas? Minimum distance between any two points on their borders? – mbeckish Oct 18 '13 at 18:42
  • 2
    Searching for library/API is generally offtopic...but please consider searching for similar questions before posting you new one... – Alexei Levenkov Oct 18 '13 at 18:44

3 Answers3

3

If you're using sql 2008 or later you can use the sys.geography data type to store your locational data in the database and let the server do the calculating for you. That way you can save your API calls for other things.

This works if you're using some sort of geocoding service like Google Maps or Bing to get and store the locational data of your zip codes.

I've done this with a simple stored procedure to great effect in my latest project

CREATE PROCEDURE [dbo].[RadiusSearch]
    @point varchar(500),
    @distance int
AS
begin
    declare @geoPoint geography
    set @geoPoint = geography::STGeomFromText('POINT (' + @point + ')', 4326).STBuffer(@distance / 0.0006213712)
    select [Whatever fields and inner joins you need to get the info you wanted]
    where ClientZipCodes.Geolocation.STIntersects(@geoPoint) = 1
end

So basically what I'm doing is passing in the point data as a string and converting it into binary geographical point data (because in this instance I'm setting off the stored proc from an asynchronous javascript call from a web page, this is unnecessary if you're querying geographical data directly from the database), setting the distance variable to miles (this requires conversion because the default measurement is in meters) and then telling it to give me whatever records have Geolocation points that intersect the buffer we just set up. Easy peezy and quick to boot.

Dan S.
  • 173
  • 1
  • 14
2

Better solution:

Use Google Maps Places API and insert your customer's custom locations as given here

Then you can request for a list of your custom places within certain radius and even sorted by how close they are to the current place using this simple http url

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

where paramerters would be radius in meters and optionally rankby distance if you want it sorted accorting to distance..That would return JSON with all your customer locations within that radius

Refer this to learn how to search


Apart from this using Google maps API you would be able to show a circle on map with the specified radius and all the customers within it..That would look cool..

Anirudha
  • 32,393
  • 7
  • 68
  • 89
1

You can get free ZIP code database (I'm not providing link, you can do it via google) with latitudes and longitudes inside, then you can easily calculate distance between each zip code in miles, then you will be able to select all zipcodes in any radius

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57