How to obtain longitude and latitude for a street address programmatically (and legally)

94,871

Solution 1

The term you're looking for is geocoding and yes Google does provide this service.

Solution 2

In addition to the aforementioned Google geocoding web service, there is also a competing service provided by Yahoo. In a recent project where geocoding is done with user interaction, I included support for both. The reason is I have found that, especially outside the U.S., their handling of more obscure locations varies widely. Sometimes Google will have the best answer, sometimes Yahoo.

One gotcha to be aware of: if Google really thinks they don't know where your place is, they will return a 602 error indicating failure. Yahoo, on the other hand, if it can peel out a city/province/state/etc out of your bad address, will return the location of the center of that town. So you do have to pay attention to the results you get to see if they are really what you want. There are ancillary fields in some results that tell you about this: Yahoo calls this field "precision" and Google calls it "accuracy".

Solution 3

If you want to do this without relying on a service, then you download the TIGER Shapefiles from the US Census.

You look up the street you're interested in, which will have several segments. Each segment will have a start address and end address, and you interpolate along the segment to find where on the segment your house number lies.

This will provide you with a lon/lat pair.

Keep in mind, however, that online services employ a great deal of address checking and correction, which you'd have to duplicate as well to get good results.

Also note that as nice as free data is, it's not perfect - the latest streets aren't in there (they might be in the data Google uses), and the streets may be off their real location by some amount due to survey inaccuracies. But for 98% of geocoding needs it works perfectly, is free, and you control everything so you're reducing dependencies in your app.

Openstreetmaps has the aim of mapping everything in the world, though they aren't quite there it's worth keeping tabs on as they provide their data under a CC license

However, many (most?) other countries are only mapped by gov't or services for which you need to pay a fee. If you don't need to geocode very much data, then using Google, Yahoo, or some of the other free worldwide mapping services may be enough.

If you have to geocode a lot of data, then you will be best served by leasing map data from a major provider, such as teleatlas.

-Adam

Solution 4

You could also try the OpenStreetMap NameFinder, which contains open source, wiki-like street data for (potentially) the entire world. NameFinder will cease to exist at the end of august, but Nominatim is its replacement.

Solution 5

Google requires you to show a Google map with their data, with a max of 2.5k (was 25k) HTTP requests per day. Useful but you have to watch usage.

They do have

http://groups.google.com/group/Google-Maps-API/web/resources-non-google-geocoders

(Google has since removed this. If you see a duplicate or cache, I'll link to that.)

...in which I found GeoNames which has both a downloadable db, free web service and a commercial web service.

Share:
94,871
Ola Eldøy
Author by

Ola Eldøy

I wrote a game in assembly language for the Dragon 32/64 back in 1991. Dataflex developer since 1998. Twitter LinkedIn Data Access Worldwide Forums

Updated on October 31, 2020

Comments

  • Ola Eldøy
    Ola Eldøy over 3 years

    Supposedly, it is possible to get this from Google Maps or some such service. (US addresses only is not good enough.)

  • Ola Eldøy
    Ola Eldøy over 15 years
    Excellent! The section "Geocoding via HTTP" seems to be what I'm looking for.
  • Lazy
    Lazy over 14 years
    This wouldn't work (for this question; in general it sounds fine for the US), as the question already states that a solution that only works for US addresses would not be an acceptable solution.
  • Adam Davis
    Adam Davis over 14 years
    That's true, but originally the question did not state this - edits made by the author within 15 minutes of the original post are not considered edits. Still, I'll update the answer.
  • Chris Moschini
    Chris Moschini over 12 years
    A jsfiddle with an example of the replacement for the Yahoo service, Nokia's ovimaps: jsfiddle.net/MgjGr
  • Lazy
    Lazy over 11 years
    Just be sure that you keep within the usage rules. I think Google only allows the use of this if you show the locations on a Google Map. It is not allowed to use it for other purposes, but you should check the licence and usage rules.
  • Funka
    Funka over 11 years
    Unfortunately it seems like the groups.google.com link no longer exists... Thank you for the geonames link however, looks promising!
  • Funka
    Funka over 11 years
    v2 (deprecated since 2010) is shutting down completely in just a few months (May 2013), so recommend to not build anything new with this one anymore!
  • Funka
    Funka over 11 years
    Point of correction/clarification: Google "v3" API allows 2,500 requests per day (per IP address!), not 25k. See developers.google.com/maps/documentation/geocoding/#Limits
  • Rajesh
    Rajesh about 9 years
    Super ! , Thank god read all the answer ,thats y i can find your answer. Shall i know is there any restrictions of using this url in normal php . like number of request per day ? @Nate
  • Gurupad Hegde
    Gurupad Hegde over 8 years
    Above link seems like broken
  • Nhan
    Nhan almost 8 years
    Obtaining it through the service is allowed but storing it for later use is not. See the comment (under the answer) about it here stackoverflow.com/a/30595524/2403881