Get country from coordinates?

后端 未结 4 1745
面向向阳花
面向向阳花 2020-12-08 03:15

How to get country name in Android, If are known geo coordinates? How to make it the simplest way?

4条回答
  •  攒了一身酷
    2020-12-08 03:23

    How I did it recently was to read the data from the Web API URL and parse the JSON.

    An example URL for the point(40.714224, -73.961452) is:

    http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
    

    Which produces the following output:

    {
      "name": "40.714224,-73.961452",
      "Status": {
        "code": 200,
        "request": "geocode"
      },
      "Placemark": [ {
        "id": "p1",
        "address": "285 Bedford Ave, Brooklyn, NY 11211, USA",
        "AddressDetails": {
       "Accuracy" : 8,
       "Country" : {
          "AdministrativeArea" : {
             "AdministrativeAreaName" : "NY",
             "SubAdministrativeArea" : {
                "Locality" : {
                   "DependentLocality" : {
                      "DependentLocalityName" : "Williamsburg",
                      "PostalCode" : {
                         "PostalCodeNumber" : "11211"
                      },
                      "Thoroughfare" : {
                         "ThoroughfareName" : "285 Bedford Ave"
                      }
                   },
                   "LocalityName" : "Brooklyn"
                },
                "SubAdministrativeAreaName" : "Kings"
             }
          },
          "CountryName" : "USA",
          "CountryNameCode" : "US"
       }
    },
        "ExtendedData": {
          "LatLonBox": {
            "north": 40.7154779,
            "south": 40.7127799,
            "east": -73.9600584,
            "west": -73.9627564
          }
        },
        "Point": {
          "coordinates": [ -73.9614074, 40.7141289, 0 ]
        }
      } ]
    }
    

    I find GSON to be quite good for parsing JSON in Android.

提交回复
热议问题