Get country from coordinates?(从坐标获取国家?)
问题描述
如何在Android中获取国家名称,如果已知地理坐标?怎么做最简单?
How to get country name in Android, If are known geo coordinates? How to make it the simplest way?
推荐答案
为此使用下面的函数.
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 + "
" + obj.getCountryName();
add = add + "
" + obj.getCountryCode();
add = add + "
" + obj.getAdminArea();
add = add + "
" + obj.getPostalCode();
add = add + "
" + obj.getSubAdminArea();
add = add + "
" + obj.getLocality();
add = add + "
" + 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"/>
这篇关于从坐标获取国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!