【问题标题】:How to show location name when i have the longitude and latitude?当我有经度和纬度时如何显示位置名称?
【发布时间】:2013-04-03 13:55:18
【问题描述】:

当我有位置的经度和纬度时,我试图从 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();
        }
    }
   }

【问题讨论】:

  • 代码似乎正确。你有什么错误吗?请更好地描述您的问题。
  • List
    地址 = geocoder.getFromLocation(lat, longi, 1);在此声明中,地址为空。
  • 尝试反向地理编码并检查此链接mobiforge.com/developing/story/using-google-maps-android
  • 只需要使用经度和纬度的地址名称,可以作为短信发送给其他用户。

标签: android google-maps google-geocoder


【解决方案1】:

我想你差不多完成了,我之前的处理方法是使用以下代码:

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

【讨论】:

  • 它以字符串形式返回最终地址名称,因此您可以发送短信给其他人
  • 您是否将Locale 参数更改为您在new Geocoder(context, Locale.CHINA); 中的位置?
猜你喜欢
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多