从坐标获取国家/地区?

问题描述 投票:0回答:5

如果已知地理坐标,如何在 Android 中获取国家/地区名称?怎么做最简单?

android google-maps geolocation gps
5个回答
32
投票

给你

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);

    if (addresses.size() > 0)
     {  
       String countryName=addresses.get(0).getCountryName();
     }

24
投票

使用下面的函数。

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

将以下权限添加到您的清单中

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

8
投票

用这个

try {
        Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses.isEmpty()) {
            placeName.setText("Waiting for Location");
        }
        else {
            if (addresses.size() > 0) {
                placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
            }
        }
    }
    catch(Exception e){
        Toast.makeText(this, "No Location Name Found", 600).show();
    }

在清单文件中使用它

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2
投票

我最近的做法是从 Web API URL 读取数据并解析 JSON。

点(40.714224,-73.961452)的示例 URL 是:

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

产生以下输出:

{
  "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 ]
    }
  } ]
}

我发现 GSON 非常适合在 Android 中解析 JSON。


0
投票

这里有一种方法也可以离线工作并且不花费 API 钱:

import io.github.coordinates2country.Coordinates2Country;

[...]

Coordinates2Country.country(-23.7, 39.8); // returns the String "France".

您需要导入库,例如使用 Gradle:

implementation("io.github.coordinates2country:coordinates2country:1.7")
  • 开源。
  • 仅需 50 毫秒即可获得结果,比任何 API 都快。
  • JAR 小于 100kB。
  • 免责声明:由我制作。

https://github.com/coefficients2country/coefficients2country-android

© www.soinside.com 2019 - 2024. All rights reserved.