Android 使用地理编码器显示用户';地址

Android 使用地理编码器显示用户';地址,android,Android,我使用此方法在地图活动上显示用户的纬度和经度: public void animateMap(Location location){ double lat = location.getLatitude(); double lng = location.getLongitude(); Toast.makeText(MyMapActivity.this, "Sie sind an\n" + lat + "\n" + ln

我使用此方法在地图活动上显示用户的纬度和经度:

public void animateMap(Location location){
        double lat = location.getLatitude();
        double lng = location.getLongitude();

       Toast.makeText(MyMapActivity.this,
                "Sie sind an\n" + lat + "\n" + lng, Toast.LENGTH_SHORT)
                .show();
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        mapController.animateTo(point, new Message());
        mapOverlay.setPointToDraw(point);

    }

如何在我的方法上实现地理编码器?因此Toast将显示位置的地址而不是坐标

最简单的实现是使用geocoder类:

 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();
Geocoder-Geocoder=新的地理编码器(context,Locale.ENGLISH);
地理编码器。获取地理位置(lat、lng、1);
List ls=新的ArrayList();
ls=地理编码器。从位置获取(纬度,液化天然气,1);
字符串myLocation=ls.get(0.getSubAdminArea();
您可以检查该类返回的所有信息,并选择最适合您的信息。它包含从国家名称到地标名称、邻居、邮政编码。。。几乎任何你可能需要的东西

但是请记住,如果谷歌没有关于这个位置的信息,它将返回一个空字符串

所以对于你来说,例子应该是这样的:

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);

}
public void animateMap(位置){
双纬度=location.getLatitude();
double lng=location.getLongitude();
串定位;
试一试{
Geocoder Geocoder=新的Geocoder(上下文,Locale.ENGLISH);
地理编码器。获取地理位置(lat、lng、1);
List ls=新的ArrayList();
ls=地理编码器。从位置获取(纬度,液化天然气,1);
myLocation=ls.get(0.getSubAdminArea();
}捕获(例外e){
myLocation=“对不起,我们没有关于此位置的信息”;
}
Toast.makeText(MyMapActivity.this,
“Sie sind an\n”+我的位置,吐司。长度(短)
.show();
地质点=新的地质点((内部)(纬度*1E6),(内部)(液化天然气*1E6));
animateTo(点,新消息());
mapOverlay.setPointToDraw(点);
}

从Geocoder获取您所在位置的地址列表,并检查:

Geocoder-Geocoder=new-Geocoder(getApplicationContext,Locale.getDefault());
列表地址=geocoder.getFromLocation(lat,long,1);
字符串myLocation=“”;
if(address!=null&&!address.isEmpty())
{
myLocation=address.get(0.getSubAdminArea();
}
您使用

 Geocoder myLocation = new Geocoder(context, Locale.ENGLISH);
List<Address> myList= myLocation.getFromLocation(lat, lng, 1);
Address add = myList.get(0);
String addressString = add.getAddressLine(0);
String country = add.getCountryName();                      
String city = add.getLocality();
Geocoder myLocation=新的地理编码器(context,Locale.ENGLISH);
列表myList=myLocation.getFromLocation(lat,lng,1);
地址add=myList.get(0);
String addressString=add.getAddressLine(0);
字符串country=add.getCountryName();
字符串city=add.getLocality();

太糟糕了,我的位置为空。有什么解决方案吗?subadminarea是一个非常详细的信息,您可以尝试adminarea等,这样您至少可以在此位置获得一些基本信息!当地理编码器无法从给定的纬度上找到位置时,会出现错误。应用程序将被强制关闭。
 Geocoder myLocation = new Geocoder(context, Locale.ENGLISH);
List<Address> myList= myLocation.getFromLocation(lat, lng, 1);
Address add = myList.get(0);
String addressString = add.getAddressLine(0);
String country = add.getCountryName();                      
String city = add.getLocality();