我需要获取设备当前的地理位置。
场景:如果我在US
..:
my device location should be US
和我在的时候的情况一样UK
:
my device location should be UK
我需要用于查找此地理位置的示例代码。(location determined by Network Location Provider
)
如果您想获取当前的国家/地区代码,您需要查看Geocoder http://developer.android.com/reference/android/location/Geocoder.html你可以用它来获得Address http://developer.android.com/reference/android/location/Address.html它提供了一个getCountryCode http://developer.android.com/reference/android/location/Address.html#getCountryCode%28%29方法。像这样的东西
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();
}
至于获取当前的纬度和经度,有大量关于如何做到这一点的信息
- 如何在 Android 中以编程方式获取当前 GPS 位置? https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android
- 在 Android 上获取用户当前位置的最简单、最可靠的方法是什么? https://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a
- http://developer.android.com/guide/topics/location/index.html http://developer.android.com/guide/topics/location/index.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)