0

当我有位置的经度和纬度时,我试图从 android 中的 google maps api 中找出位置名称。我想要实现的是,在获得位置名称后,我想向我的朋友发送一条短信,告诉他们我当前的位置。我不确定是否需要打开地理编码器服务。

这就是我到目前为止所做的,现在我卡住了。

class mylocationlistener implements LocationListener
{


    @SuppressLint("NewApi")
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if(location != null)
        {
            Double longi = location.getLongitude();
            Double lat = location.getLatitude();
            String str = "";

            str= "Longitude=" + longi + ", Latitude=" + lat;
            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();

            Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());

            boolean abc = Geocoder.isPresent();
            try {
                List<Address> addresses = geocoder.getFromLocation(lat, longi, 1);
                if(!addresses.isEmpty())
                {
                str = addresses.get(0).getLocality() + addresses.get(0).getAddressLine(1)+ addresses.get(0).getAddressLine(2);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(),str, Toast.LENGTH_LONG).show();
        }
    }
   }
4

1 回答 1

0

我想你几乎已经完成了,我之前处理这个问题的方法是使用以下代码:

public static String getAddressStringFromLocation(Context context, Location location) {
    GeoPoint gp = new GeoPoint((int) (location.getLatitude() * 1e6), 
            (int) (location.getLongitude() * 1e6)); 
    return getAddressStringFromGeoPoint(context, gp);
}

public static String getAddressStringFromGeoPoint(Context context, GeoPoint point) {
    StringBuilder sb = new StringBuilder();
    Address ad = getAddressFromGeoPoint(context, point);
    if(ad != null && ad.getMaxAddressLineIndex() > 0) {
        for(int i = 0, max = ad.getMaxAddressLineIndex(); i < max; i++) {
            sb.append(ad.getAddressLine(i));
        }
        sb.append(ad.getThoroughfare());
        return sb.toString();
    } else {
        return null;
    }
}

public static Address getAddressFromGeoPoint(Context context, GeoPoint point){
    Geocoder geoCoder = new Geocoder(context, Locale.CHINA);
    Address address = null;
    try {
        List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6, 1);
        address = addresses.get(0);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return address;
}
于 2013-04-03T14:01:22.450 回答