地理coder.getFromLocationName只返回空地理、coder、getFromLocationName

2023-09-13 00:54:51 作者:别说谁变了你拦得住时间吗

我会出我的脑海过去两天的抛出:IllegalArgumentException 错误我试图让一个坐标,一个地址时,收到的Andr​​oid code ,甚至逆转,拿到地址了经度和纬度。这是code,但我看不到一个错误。这是一个标准的code段在谷歌搜索很容易发现这一点。

I am going out of my mind for the last two days with an IllegalArgumentException error I receive in Android code when trying to get a coordinates out of an address, or even reverse, get address out of longitude and latitude. This is the code, but I cannot see an error. It is a standard code snippet that is easily found on a Google search.

public GeoPoint determineLatLngFromAddress(Context appContext, String strAddress) {
    Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
    GeoPoint g = null; 
    try {
        System.out.println("str addres: " + strAddress);
        List<Address> addresses = geocoder.getFromLocationName(strAddress, 5);
        if (addresses.size() > 0) {
            g = new GeoPoint(
               (int) (addresses.get(0).getLatitude() * 1E6),
               (int) (addresses.get(0).getLongitude() * 1E6)
            );
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("locationName == null");
    }
    return g;
 }

这些都是从manifest.xml文件的权限:

These are the permissions from manifest.xml file:

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

我已经做了谷歌API密钥宣布过:&LT;使用库机器人:名称=com.google.android.maps/&GT;

从code段以上,地理codeR 不是,无论是地址 appContext ,和我绊倒在这里:地理coder.getFromLocationName(strAddress ,5);

From the code snippet above, geocoder is not null, neither is the address or appContext, and I stumble here: geocoder.getFromLocationName(strAddress, 5);

我做了很多谷歌的搜索,并没有发现任何的工作,而且我发现最重要的信息是这样的:

I did a lot of Google searching and found nothing that worked, and the most important info I found is this:

中的地缘codeR类需要未包含在核心Android框架后端服务。

The Geocoder class requires a backend service that is not included in the core android framework.

的sooo,我现在confuzed。我有什么打电话,导入,添加,在code使用....,使这项工作? 我使用谷歌API 2.2 API 8级。 如果有人已经找到了一个解决方案,或者一个指针文件,东西,我没有发现,请告诉我们。

Sooo, I am confuzed now. What do I have to call, import, add, use in code.... to make this work? I am using Google API 2.2, API level 8. If somebody has found a solution for this, or a pointer for documentation, something that I didn't discover, please let us know.

推荐答案

我也有类似的问题,发现轮询地理codeR直到我得到的结果工作。下面是我是如何做到的,到目前为止的伟大工程。

I had a similar problem and found that polling the Geocoder until i got a result worked. Here is how i did it, so far works great.

try {
    List<Address> geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    while (geoResults.size()==0) {
        geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    }
    if (geoResults.size()>0) {
        Address addr = geoResults.get(0);
        myLocation.setLatitude(addr.getLatitude());
        myLocation.setLongitude(addr.getLongitude());
    }
} catch (Exception e) {
    System.out.print(e.getMessage());
}