2

I would like to use an API (preferred to be google maps if it's supported) in my android application to pass the longitude/latitude of a location and the API returns the city name.

In the solution to this, you should go for getAddressLine(x)

mohamede1945
  • 7,092
  • 6
  • 47
  • 61

3 Answers3

2

i believe google does provide what you are requesting here http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

plague
  • 1,908
  • 11
  • 11
2

I believe that the APIs are rate limited. i.e you cannot hit the API for more than a number of times in a hour.. Android contains a Geocoder class which does ReverseGeocoding as well.. Take a look at this link.

Varun
  • 33,833
  • 4
  • 49
  • 42
  • Thanks, I think this is what I need. I'm going to test it now. – mohamede1945 Jul 17 '11 at 12:42
  • I tested it, but I cannot use it to get the city. Do you know a way to do this? – mohamede1945 Jul 17 '11 at 12:55
  • You need to have the internet permission in the manifest. `Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List
    addresses = geocoder.getFromLocation(lat, lng, 1); addresses.get(0).getLocality();` Locality should give you the city.
    – Varun Jul 18 '11 at 06:40
  • Thanks Varun! I already have the internet permission. But it gives me null. – mohamede1945 Jul 18 '11 at 12:15
  • possibly the lat/long are invalid. you can enter the lat/long on the google maps on the desktop browser and confirm that there are results for the lat/long values. – Varun Jul 18 '11 at 16:17
  • Here is the code I use Geocoder geocoder = new Geocoder(this); Address a = geocoder.getFromLocation(33.035810, -96.704071, 1).get(0); I don't think the lat/long are invalid, do you think? – mohamede1945 Jul 20 '11 at 01:13
  • tried this on the google maps. Seems to be valid. are you testing on a device/emulator. that could be a prob, not sure.. I had always tested the goecoding/reverse geocoding on a device and always got this working. There should be something very small which is missing in here.. – Varun Jul 20 '11 at 06:09
  • `07-20 14:52:22.820: VERBOSE/TAG(2921): Address[addressLines=[0:"2800 N Central Expy",1:"Plano, TX 75074",2:"USA"],feature=2800,admin=Texas,sub-admin=null,locality=Plano,thoroughfare=N Central Expy,postalCode=75074,countryCode=US,countryName=United States,hasLatitude=true,latitude=33.0356359,hasLongitude=true,longitude=-96.703987,phone=null,url=null,extras=null]` i tried the same code and the result i got it above... What is the target you tested on.. or which device? – Varun Jul 20 '11 at 09:24
  • I'm testing on HTC Desire HD, and I'm in Egypt. – mohamede1945 Jul 21 '11 at 10:03
  • You're right, the problem was from my side. Thank you I'll mark it as an answer. – mohamede1945 Jul 23 '11 at 23:52
  • pls let all know what was the problem so that others not waste time.. :D – Varun Jul 24 '11 at 14:17
1

Geonames.org is another. Here's a sample API call to find names near Seattle: http://api.geonames.org/citiesJSON?north=47.65&south=47.55&east=-122.30&west=-122.40&lang=de&username=demo

{
    "geonames": [
        {
            "fcodeName": "seat of a second-order administrative division",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Seattle",
            "wikipedia": "",
            "lng": -122.3320708,
            "fcode": "PPLA2",
            "geonameId": 5809844,
            "lat": 47.6062095,
            "population": 608660
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Yesler Terrace",
            "wikipedia": "",
            "lng": -122.3159591,
            "fcode": "PPL",
            "geonameId": 5816674,
            "lat": 47.6009318,
            "population": 0
        },
        {
            "fcodeName": "section of populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "North Broadway",
            "wikipedia": "",
            "lng": -122.3204044,
            "fcode": "PPLX",
            "geonameId": 5804929,
            "lat": 47.6462094,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Capitol Hill",
            "wikipedia": "",
            "lng": -122.3195706,
            "fcode": "PPL",
            "geonameId": 5789123,
            "lat": 47.6234317,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Interbay",
            "wikipedia": "",
            "lng": -122.3959623,
            "fcode": "PPL",
            "geonameId": 5798384,
            "lat": 47.641209,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Ross (historical)",
            "wikipedia": "",
            "lng": -122.3623502,
            "fcode": "PPL",
            "geonameId": 5808844,
            "lat": 47.6478758,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Stevens",
            "wikipedia": "",
            "lng": -122.3066667,
            "fcode": "PPL",
            "geonameId": 7153940,
            "lat": 47.6297222,
            "population": 0
        },
        {
            "fcodeName": "section of populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Belltown",
            "wikipedia": "",
            "lng": -122.3509605,
            "fcode": "PPLX",
            "geonameId": 5786913,
            "lat": 47.6164871,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "Queen Anne",
            "wikipedia": "",
            "lng": -122.366239,
            "fcode": "PPL",
            "geonameId": 5807648,
            "lat": 47.6359314,
            "population": 0
        },
        {
            "fcodeName": "populated place",
            "countrycode": "US",
            "fcl": "P",
            "fclName": "city, village,...",
            "name": "South Seattle",
            "wikipedia": "",
            "lng": -122.312903,
            "fcode": "PPL",
            "geonameId": 5811509,
            "lat": 47.5614876,
            "population": 0
        }
    ]
}
Pete Doyle
  • 937
  • 7
  • 11