Android:反向地理编码 - getFromLocation

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/472313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:11:25  来源:igfitidea点击:

Android: Reverse geocoding - getFromLocation

androidreverse-geocodingstreet-address

提问by Chrispix

I am trying to get an address based on the long/lat. it appears that something like this should work?

我正在尝试根据长/纬度获取地址。似乎这样的事情应该有效?

Geocoder myLocation = Geocoder(Locale.getDefault());
    List myList = myLocation.getFromLocation(latPoint,lngPoint,1);

The issue is that I keep getting : The method Geocoder(Locale) is undefined for the type savemaplocation

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

Any assistance would be helpful. Thank you.

任何帮助都会有所帮助。谢谢你。



Thanks, I tried the context, locale one first, and that failed and was looking at some of the other constructors (I had seen one that had mentioned just locale). Regardless,

谢谢,我首先尝试了上下文,语言环境第一个,但失败了,正在查看其他一些构造函数(我见过一个只提到语言环境的构造函数)。不管,

It did not work, as I am still getting : The method Geocoder(Context, Locale) is undefined for the type savemaplocation

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

I do have : import android.location.Geocoder;

我有: import android.location.Geocoder;

回答by Wilfred Knievel

The following code snippet is doing it for me (lat and lng are doubles declared above this bit):

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

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

回答by EricLarch

Here is a full example code using a Thread and a Handler to get the Geocoder answer without blocking the UI.

这是一个完整的示例代码,使用线程和处理程序在不阻塞 UI 的情况下获取地理编码器答案。

Geocoder call procedure, can be located in a Helper class

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

Here is the call to this Geocoder procedure in your UI Activity:

这是在您的 UI 活动中调用此 Geocoder 过程:

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

And the handler to show the results in your UI:

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

Don't forget to put the following permission in your Manifest.xml

不要忘记在您的 Manifest.xml

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

回答by Reto Meier

It looks like there's two things happening here.

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

1) You've missed the newkeyword from before calling the constructor.

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

2) The parameter you're passing in to the Geocoder constructor is incorrect. You're passing in a Localewhere it's expecting a Context.

2) 您传递给 Geocoder 构造函数的参数不正确。你正在传递一个Locale它期待一个Context.

There are two Geocoderconstructors, both of which require a Context, and one also taking a Locale:

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

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

Solution

解决方案

Modify your code to pass in a valid Context and include newand you should be good to go.

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

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

Note

笔记

If you're still having problems it may be a permissioning issue. Geocoding implicitly uses the Internet to perform the lookups, so your application will require an INTERNETuses-permission tag in your manifest.

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

Add the following uses-permission node within the manifestnode of your manifest.

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

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

回答by Ingo

The reason for this is the non-existent Backend Service:

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

The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform.

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

回答by Antony

First get Latitude and Longitude using Location and LocationManager class. Now try the code below for Get the city,address info

首先使用 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());
    }

City info is now in sb. Now convert the sb to String (using sb.toString() ).

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

回答by Chrispix

Well, I am still stumped. So here is more code.

好吧,我仍然很难过。所以这里有更多的代码。

Before I leave my map, I call SaveLocation(myMapView,myMapController);This is what ends up calling my geocoding information.

在我离开我的地图之前,我调用SaveLocation(myMapView,myMapController);这就是最终调用我的地理编码信息的原因。

But since getFromLocationcan throw an IOException, I had to do the following to call SaveLocation

但是由于getFromLocation可以抛出一个IOException,我必须执行以下操作才能调用 SaveLocation

try
{
    SaveLocation(myMapView,myMapController);
}
catch (IOException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Then I have to change SaveLocation by saying it throws IOExceptions :

然后我必须通过说它抛出 IOExceptions 来更改 SaveLocation :

 public void SaveLocation(MapView mv, MapController mc) throws IOException{
    //I do this : 
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
//...
    }

And it crashes every time.

而且每次都崩溃。

回答by Chrispix

I've heard that Geocoder is on the buggy side. I recently put together an example application that uses Google's http geocoding service to lookup location from lat/long. Feel free to check it out here

我听说 Geocoder 有问题。我最近整理了一个示例应用程序,该应用程序使用 Google 的 http 地理编码服务从经纬度查找位置。随意在这里检查一下

http://www.smnirven.com/?p=39

http://www.smnirven.com/?p=39

回答by Sojan Ks

Use this

用这个

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