21

I'm trying to find a webservice that will allow me to get a County name (not Country) for a specific Lat/Long. I would be performing the lookup within a server application (likely a Java application). It doesn't have a be a webservice if there is some library out there I suppose, but I would like up-to-date information. I have looked around quite a bit for an API that supports this, but so far I haven't been able to find one that works. I have tried the Yahoo APIs like so:

http://where.yahooapis.com/geocode?q=39.76144296429947,%20-104.8011589050293

But it doesn't populate the address information. I've tried with some of the "flags" options there too to no avail.

I've also looked around at Googles APIs as well, but I've read multiple places that they don't populate the County.

So does anyone know of any APIs that will take a Lat/Long and return the County associated with that location? And if you have any examples, that would be great.

I'd also like to know which APIs allow for use in a commercial application. A lot of the data I've found says that you can't use the data to make money. I might be reading those wrong, but I'm looking to build a service that I'd likely charge for that would use this data. So I'd need options. Maybe free services while I'm exploring options, and pay services down the road.

Kara
  • 6,115
  • 16
  • 50
  • 57
Andrew Serff
  • 2,117
  • 4
  • 21
  • 32

5 Answers5

30

Just for completeness, I found another API to get this data that is quite simple, so I thought I'd share.

https://geo.fcc.gov/api/census/

The FCC provides an Block API for exactly this problem and it uses census data to perform the look up.

Their usage limit policy is (From developer@fcc.gov)

We do not have any usage limits for the block conversion API, but we do ask that you try to spread out your requests over time if you can.

CodeJunkie
  • 339
  • 3
  • 16
Andrew Serff
  • 2,117
  • 4
  • 21
  • 32
18

Google does populate the county for your example,

http://maps.googleapis.com/maps/api/geocode/json?latlng=39.76144296429947,-104.8011589050293&sensor=false

In the response, look under the key address_components which contains this object representing "Adams" county,

{
    long_name: "Adams"
    short_name: "Adams"
    -types: [
        "administrative_area_level_2"
        "political"
    ]
}

Here's from the Geocoding API's docs,

administrative_area_level_2 indicates a second-order civil entity below the country level. Within the United States, these administrative levels are counties. Not all nations exhibit these administrative levels.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • Thanks for the info! That looks like it will work as long as it is populated all the time. I'll have to do some more testing. Any idea on the licensing of googles map data? – Andrew Serff May 04 '11 at 03:30
  • 2
    Google only allows you to use their reverse geocoding in conjunction with an embedded Google Map. – James D May 08 '12 at 21:20
  • James D> What do you mean by 'Google only allows' - its just a http call, so how would google or anyone determine wat purpose results were used for? or did u mean something else. – Jasper May 07 '15 at 13:01
  • @Jasper - It's part of the Terms of Service for using that API. Taken from the docs on [Usage Limits](https://developers.google.com/maps/documentation/geocoding/?csw=1#Limits), "The Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions." – Anurag May 07 '15 at 17:24
  • Anurag> Now, if you want to simply get address based on lat and long (and not use in map)- thats not allowed as per terms n cond. ? If so, what is the alternative? – Jasper May 07 '15 at 17:52
  • @Jasper - yes, that's correct. I haven't read the entire Terms of Service so I can't be 100% on this, but I'm pretty sure that you need to show the results of the geocoding on a Google map. The alternative would be to use a different service. I think OpenStreetMap might work - see this thread - https://help.openstreetmap.org/questions/11744/is-there-a-osm-geocoder-equivalent-to-google-maps-geocoder – Anurag May 08 '15 at 06:00
  • Thankyou Anurag, apparently Android's very own GeoCoder api does the job too if an implementation exists. – Jasper May 08 '15 at 06:18
2

You may want to have look at Tiger data and see if it has polygons containing the county name in an attribute. If it does the Java Geotools API lets you work with this data. You will be performing point in polygon queries for the county polygons followed by a feature attribute look-up.

mpr
  • 3,250
  • 26
  • 44
whatnick
  • 5,400
  • 3
  • 19
  • 35
  • Thanks! That is good info to have access to for sure. I'm going to try out the google apis for now, but his looks promising too and removes the external dependency. – Andrew Serff May 04 '11 at 03:34
2

Another option:

  • Download the cities database from http://download.geonames.org/export/dump/
  • Add each city as a lat/long -> Country mapping to a spatial index such as an R-Tree (some DBs also have the functionality)
  • Use nearest-neighbour search to find the country corresponding to the closest human settlement for any given point

Advantages:

  • Does not depend on aa external server to be available
  • Much faster (easily does thousands of lookups per second)

Disadvantages:

  • May give wrong answers close to borders, especially in sparsely populated areas
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Maybe this is a great solution.It is in a json format.I always use this in my projects.

http://maps.google.com/maps/geo?ll=10.345561,123.896932

And simply extract the information using php.

$x = file_get_contents("http://maps.google.com/maps/geo?ll=10.345561,123.896932");

$j_decodex = json_decode($x);

print_r($j_decodex);
spicykimchi
  • 1,151
  • 5
  • 22
  • 41
  • Is there any difference between using maps.google.com and the maps.googleapis.com? I would assume that I should use the geocoding apis rather than maps.google.com, but just wondering if there is any difference. – Andrew Serff May 04 '11 at 03:32
  • Just use which is pretty easy for you. – spicykimchi May 04 '11 at 05:32
  • FYI, Google's ToS only allow you to use their geocoding API to produce results you're using on one of their maps. – Brad Koch May 02 '13 at 16:18
  • 1
    { "Status": { "code": 610, "request": "geocode", "error_message": "The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/" } } – Jatin Dhoot Oct 04 '14 at 07:17