最簡單的實現方式是通過利用地理編碼器類:
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
geocoder.getFromLocation(lat, lng, 1);
List<Address> ls=new ArrayList();
ls= geocoder.getFromLocation(lat, lng, 1);
String myLocation = ls.get(0).getSubAdminArea();
您可以檢查這個類返回的所有信息,並選擇哪一個最適合你。它包含國家名稱,地標名稱,鄰居郵政編碼......幾乎任何你可能需要的。
但請記住,如果谷歌沒有關於這個位置的信息將返回一個空字符串!因此,對於你
exapmple應該是類似的東西:
public void animateMap(Location location){
double lat = location.getLatitude();
double lng = location.getLongitude();
String myLocation;
try{
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
geocoder.getFromLocation(lat, lng, 1);
List<Address> ls=new ArrayList();
ls= geocoder.getFromLocation(lat, lng, 1);
myLocation = ls.get(0).getSubAdminArea();
}catch(Exception e){
myLocation="Sorry, we have no information about this location";
}
Toast.makeText(MyMapActivity.this,
"Sie sind an\n" + myLocation , Toast.LENGTH_SHORT)
.show();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(point, new Message());
mapOverlay.setPointToDraw(point);
}
GeoCoder無法從給定位置找到位置時會發生錯誤。並且應用程序將被迫關閉。 – 2012-07-19 05:27:03