Android:反向地理编码-getFromLocation

Android:反向地理编码-getFromLocation,android,reverse-geocoding,street-address,Android,Reverse Geocoding,Street Address,我正在尝试获取基于long/lat的地址。看起来这样的东西应该可以工作吗 Geocoder myLocation = Geocoder(Locale.getDefault()); List myList = myLocation.getFromLocation(latPoint,lngPoint,1); 问题是我一直在得到:类型savemaplocation的方法Geocoder(Locale)未定义 任何援助都会有帮助。多谢各位 谢谢,我首先尝试了上下文,LocaleOne,但失败

我正在尝试获取基于long/lat的地址。看起来这样的东西应该可以工作吗

Geocoder myLocation = Geocoder(Locale.getDefault());
    List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
问题是我一直在得到:类型savemaplocation的方法Geocoder(Locale)未定义

任何援助都会有帮助。多谢各位


谢谢,我首先尝试了上下文,LocaleOne,但失败了,并且正在查看其他一些构造函数(我看到一个只提到了locale)。不管怎样

它不起作用,因为我仍在了解:类型savemaplocation的方法Geocoder(Context,Locale)未定义


我有:导入android.location.Geocoder

这里似乎发生了两件事

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

2) 传递给Geocoder构造函数的参数不正确。您正在传递一个
区域设置
,其中需要一个
上下文

有两个
Geocoder
构造函数,它们都需要
上下文
,还有一个还需要
区域设置

Geocoder(Context context, Locale locale)
Geocoder(Context context)
解决方案

修改您的代码以在有效上下文中传递,并包括
new
,这样您就可以开始了

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

嗯,我还是很困惑。这里有更多的代码

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

但是由于
getFromLocation
可以抛出
IOException
,因此我必须执行以下操作来调用SaveLocation

try
{
    SaveLocation(myMapView,myMapController);
}
catch (IOException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}
然后,我必须改变SaveLocation,说它抛出了IOExceptions:

 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);
//...
    }

它每次都会崩溃。

下面的代码片段是为我做的(lat和lng是这个位上面声明的双倍):

Geocoder-Geocoder=new-Geocoder(这个,Locale.getDefault());
列表地址=地理编码器。getFromLocation(lat,lng,1);

我听说Geocoder是有问题的。我最近编写了一个示例应用程序,它使用Google的http地理编码服务从lat/long查找位置。请随意在这里查看


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

Geocoder调用过程,可以位于帮助器类中

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中显示结果的处理程序:

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" />

首先使用Location and 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());
    }
double latitude=location.getLatitude();
double longitude=location.getLongitude();
Geocoder gc=新的Geocoder(这个,Locale.getDefault());
试一试{
列表地址=gc.getFromLocation(lat,lng,1);
StringBuilder sb=新的StringBuilder();
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i

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

原因是不存在后端服务:

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

用这个

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Geocoder-Geocoder=new-Geocoder(这个,Locale.getDefault());
列表地址=地理编码器。getFromLocation(lat,lng,1);


谢谢,我想我昨晚工作得太晚了,不知道我的新手机出了什么事。我想我本来忘了,当我只有本地语言环境时就把它放回去了,然后当我回去时,又忘了。我很感激。不用担心,我也没有第一次发现它:)地理编码课程有没有获取地址的配额?@Sniper有关于Geocoder类配额的任何答复吗?可能是权限问题。Geocoder使用internet进行查找,因此您需要internet使用权限。更新了详细信息。我在那里有用于映射的internet权限。很奇怪为什么它一直失败。让我获取一个日志。这是我得到的例外ng:java.lang.IllegalArgumentException:latitude==3.2945512e7我想我已经弄明白了,它需要的是lat/long而不是E7(即32.945512)@Chrispix什么是E7?我正在试图找出我的问题所在?我也尝试过这个,每次我尝试查看地址列表时,它都会爆炸。不确定发生了什么。我将在一两天内尝试使用一个新的应用程序,看看我能找到什么。我发现这对定位东西非常有用:…我的男人也有以下两个权限ifest:当我漏掉其中一个(不记得是哪一个)时,应用程序抛出了一个毫无帮助的错误。地理编码器类是否有获取地址的配额?2019年我们有配额吗(api>=26 SDK)在不传递上下文的情况下获取地址的一些方法?这是正确的答案。其他答案在没有后端的情况下不起作用。这在模拟器之外工作,因为Google Play服务正在运行。代码工作完美无瑕,为进一步自定义打下了良好基础。这是正确的,其他一个android将通过
NetworkMainThreadException
或者,你甚至可以使用一个更简单的
AsyncTask
来实现这个反向地理编码地址多次出现。我不知道为什么?@VineethKuttipurathkottayodan如果同一地址出现多次,那是因为你把函数getAddressFromLocation放在了onLocationChanged函数中。地理编码类f有配额吗或者获取地址?Geocoder类是否有获取地址的配额?
<uses-permission android:name="android.permission.INTERNET" />
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());
    }
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);