2

Hi there I am able to reverse geocode to a location based on lat/long values. But my problem is i just need the country and city name, nothing else.

It happens that the json data given by google keeps changing based on the available data. At times there is a locality string, but for some lat/long there is no locality information. Is there a rule of thumb or some wrapper class that can extract the city and country from all possible json strings?

for example http://maps.google.com/maps/geo?q=24.487148563173424,-78.046875&output=json gives me

{
  "name": "24.487148563173424,-78.046875",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "Central Andros, The Bahamas",
    "AddressDetails": {
   "Accuracy" : 3,
   "Country" : {
      "CountryName" : "The Bahamas",
      "CountryNameCode" : "BS",
      "SubAdministrativeArea" : {
         "SubAdministrativeAreaName" : "Central Andros"
      }
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 24.6312368,
        "south": 24.3062499,
        "east": -77.7177462,
        "west": -78.2299838
      }
    },
    "Point": {
      "coordinates": [ -77.9738650, 24.4688482, 0 ]
    }
  } ]
}

where as http://maps.google.com/maps/geo?q=47.33882269482199,-100.634765625&output=json gives me

{
  "name": "47.33882269482199, -100.634765625",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "11445 471st Ave NE, McClusky, ND 58463, USA",
    "AddressDetails": {
   "Accuracy" : 8,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "ND",
         "SubAdministrativeArea" : {
            "Locality" : {
               "LocalityName" : "McClusky",
               "PostalCode" : {
                  "PostalCodeNumber" : "58463"
               },
               "Thoroughfare" : {
                  "ThoroughfareName" : "11445 471st Ave NE"
               }
            },
            "SubAdministrativeAreaName" : "Sheridan"
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 47.3307737,
        "south": 47.3244785,
        "east": -100.6025856,
        "west": -100.6089256
      }
    },
    "Point": {
      "coordinates": [ -100.6089257, 47.3276298, 0 ]
    }
  } ]
}

Is there some standard procedure where i can get the city and country name? Right i am traversing through the json, trying to handle the different formats that gogole is throwing at me.

Thanks

4

1 回答 1

0

You can try to look for a suitable parser to extract that information?

EDIT

http://www.devshed.com/c/a/PHP/Parsing-Google-Maps-API-using-PHP-and-JSON-2348127/

I believe this is a good read for your parser

于 2011-01-17T07:15:56.417 回答