如何以编程方式获取设备的当前地理位置

Mor*_*ory 4 android geolocation

我需要获取设备当前的地理位置.

场景:如果我在US...:

my device location should be US
Run Code Online (Sandbox Code Playgroud)

与案件相同,当我在 UK:

my device location should be UK
Run Code Online (Sandbox Code Playgroud)

我需要示例代码来查找此地理位置.(location determined by Network Location Provider)

Cra*_*igy 7

如果您想获取当前的国家/地区代码,您需要查看Geocoder可以使用Address哪个来获取提供getCountryCode方法的内容.像这样的东西

Geocoder geocoder = new Geocoder(this);
List<Address> addresses = null;

try {
    addresses = geocoder.getFromLocation(
        location.getLatitude(),
        location.getLongitude(), 1);
} catch (IOException e) {
    e.printStackTrace();
}

if (addresses != null && addresses.size() > 0) {
    Address address = addresses.get(0);
    String countryCode = address.getCountryCode();
}
Run Code Online (Sandbox Code Playgroud)

至于获得当前的纬度和经度,有很多关于如何做到这一点的信息