Android:反向地理编码 - getFromLocation

Chr*_*pix 50 android reverse-geocoding street-address

我想基于long/lat得到一个地址.似乎这样的事情应该有效吗?

Geocoder myLocation = Geocoder(Locale.getDefault());
    List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
Run Code Online (Sandbox Code Playgroud)

问题是我不断得到:方法Geocoder(Locale)未定义类型savemaplocation

任何帮助都会有所帮助.谢谢.


谢谢,我尝试了上下文,首先是locale,然后失败并且正在查看其他一些构造函数(我曾经看过一个只提到locale).而不管,

它没有用,因为我仍然得到:方法Geocoder(Context,Locale)未定义类型savemaplocation

我有:import android.location.Geocoder;

Wil*_*vel 68

以下代码片段正在为我做(lat和lng是在此位上方声明的双精度数):

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Run Code Online (Sandbox Code Playgroud)

  • 我发现这非常有助于定位内容:http://blogoscoped.com/archive/2008-12-15-n14.html (4认同)

Eri*_*rch 49

下面是一个完整的示例代码,使用Thread和Handler来获取Geocoder的答案,而不会阻止UI.

Geocoder调用程序,可以位于Helper类中

public static void getAddressFromLocation(
        final Location location, final Context context, final Handler handler) {
    Thread thread = new Thread() {
        @Override public void run() {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
            String result = null;
            try {
                List<Address> list = geocoder.getFromLocation(
                        location.getLatitude(), location.getLongitude(), 1);
                if (list != null && list.size() > 0) {
                    Address address = list.get(0);
                    // sending back first address line and locality
                    result = address.getAddressLine(0) + ", " + address.getLocality();
                }
            } catch (IOException e) {
                Log.e(TAG, "Impossible to connect to Geocoder", e);
            } finally {
                Message msg = Message.obtain();
                msg.setTarget(handler);
                if (result != null) {
                    msg.what = 1;
                    Bundle bundle = new Bundle();
                    bundle.putString("address", result);
                    msg.setData(bundle);
                } else 
                    msg.what = 0;
                msg.sendToTarget();
            }
        }
    };
    thread.start();
}
Run Code Online (Sandbox Code Playgroud)

以下是您在UI活动中对此Geocoder过程的调用:

getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());
Run Code Online (Sandbox Code Playgroud)

以及在UI中显示结果的处理程序:

private class GeocoderHandler extends Handler {
    @Override
    public void handleMessage(Message message) {
        String result;
        switch (message.what) {
        case 1:
            Bundle bundle = message.getData();
            result = bundle.getString("address");
            break;
        default:
            result = null;
        }
        // replace by what you need to do
        myLabel.setText(result);
    }   
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将以下许可放入您的 Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)


Ret*_*ier 37

看起来这里发生了两件事.

1)new在调用构造函数之前,您已经错过了关键字.

2)您传入Geocoder构造函数的参数不正确.你正在经历一个Locale期待的地方Context.

有两个Geocoder构造函数,两个都需要一个Context,一个也需要Locale:

Geocoder(Context context, Locale locale)
Geocoder(Context context)
Run Code Online (Sandbox Code Playgroud)

修改你的代码以传入一个有效的上下文并包含new,你应该很高兴.

Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
Run Code Online (Sandbox Code Playgroud)

注意

如果您仍然遇到问题,可能是一个许可问题.地理编码隐式使用Internet执行查找,因此您的应用程序将需要INTERNET清单中的uses-permission标记.

manifest清单的节点中添加以下uses-permission节点.

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)


Ing*_*ngo 7

原因是不存在的后端服务:

Geocoder类需要一个未包含在核心android框架中的后端服务.如果平台中没有后端服务,则Geocoder查询方法将返回空列表.


Ant*_*ony 5

首先使用 Location 和 LocationManager 类获取纬度和经度。现在试试下面的代码获取城市,地址信息

double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
    List<Address> addresses = gc.getFromLocation(lat, lng, 1);
    StringBuilder sb = new StringBuilder();
    if (addresses.size() > 0) {
        Address address = addresses.get(0);
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
            sb.append(address.getAddressLine(i)).append("\n");
            sb.append(address.getLocality()).append("\n");
            sb.append(address.getPostalCode()).append("\n");
            sb.append(address.getCountryName());
    }
Run Code Online (Sandbox Code Playgroud)

城市信息现在在某人。现在将 sb 转换为字符串(使用 sb.toString() )。