为了账号安全,请及时绑定邮箱和手机立即绑定

Android:反向地理编码 - getFromLocation

Android:反向地理编码 - getFromLocation

白板的微信 2019-08-02 15:48:43
Android:反向地理编码 - getFromLocation我想基于long / lat得到一个地址。似乎这样的事情应该有效吗?Geocoder myLocation = Geocoder(Locale.getDefault());     List myList = myLocation.getFromLocation(latPoint,lngPoint,1);问题是我不断得到:方法Geocoder(Locale)未定义类型savemaplocation任何帮助都会有所帮助。谢谢。谢谢,我尝试了上下文,首先是locale,然后失败了,并且正在查看其他一些构造函数(我曾经看过一个只提到locale)。而不管,它没有用,因为我仍然得到:方法Geocoder(Context,Locale)未定义类型savemaplocation我有:import android.location.Geocoder;
查看完整描述

3 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

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

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


查看完整回答
反对 回复 2019-08-02
?
慕容森

TA贡献1853条经验 获得超18个赞

下面是一个完整的示例代码,使用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();}

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

getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());

以及在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);
    }   }

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

<uses-permission android:name="android.permission.INTERNET" />


查看完整回答
反对 回复 2019-08-02
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

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

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

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

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

Geocoder(Context context, Locale locale)Geocoder(Context context)

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

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

注意

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

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

<uses-permission android:name="android.permission.INTERNET" />


查看完整回答
反对 回复 2019-08-02
  • 3 回答
  • 0 关注
  • 1921 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信