geocoder.getFromLocationName 仅返回 null

发布于 2024-10-09 19:03:53 字数 1756 浏览 0 评论 0原文

在过去的两天里,当我试图从地址中获取坐标,甚至反向获取经度和纬度的地址时,我在 Android 代码中收到了 IllegalArgumentException 错误,这让我发疯了。这是代码,但我看不到错误。它是一个标准代码片段,很容易在 Google 搜索中找到。

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文件的权限:

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

我也声明了Google API密钥:

来自上面的代码片段,geocoder 不是 nulladdressappContext 也不是,我在这里绊倒了:< code>geocoder.getFromLocationName(strAddress, 5);

我进行了大量的 Google 搜索,但没有发现任何有用的信息,我发现的最重要的信息是:

Geocoder 类需要一个未包含在核心 Android 框架中的后端服务。

苏,我现在很困惑。我必须在代码中调用、导入、添加、使用什么......才能使其工作? 我正在使用 Google API 2.2,API 级别 8。 如果有人找到了这个问题的解决方案,或者文档的指针,一些我没有发现的东西,请告诉我们。

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

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

I do have the Google API key declared too: <uses-library android:name="com.google.android.maps" />

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:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-10-16 19:03:54

我遇到了类似的问题,发现轮询地理编码器直到得到结果为止。这是我的做法,到目前为止效果很好。

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

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());
}
清晨说晚安 2024-10-16 19:03:54
public LatLng determineLatLngFromAddress(Context appContext, String strAddress) {
        LatLng latLng = null;
        Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
        List<Address> geoResults = null;

        try {
            geoResults = geocoder.getFromLocationName(strAddress, 1);
            while (geoResults.size()==0) {
                geoResults = geocoder.getFromLocationName(strAddress, 1);
            }
            if (geoResults.size()>0) {
                Address addr = geoResults.get(0);
                latLng = new LatLng(addr.getLatitude(),addr.getLongitude());
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }

    return latLng; //LatLng value of address
    }
public LatLng determineLatLngFromAddress(Context appContext, String strAddress) {
        LatLng latLng = null;
        Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
        List<Address> geoResults = null;

        try {
            geoResults = geocoder.getFromLocationName(strAddress, 1);
            while (geoResults.size()==0) {
                geoResults = geocoder.getFromLocationName(strAddress, 1);
            }
            if (geoResults.size()>0) {
                Address addr = geoResults.get(0);
                latLng = new LatLng(addr.getLatitude(),addr.getLongitude());
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }

    return latLng; //LatLng value of address
    }
七秒鱼° 2024-10-16 19:03:54

有同样的问题。池化不起作用,所以我用它来获取位置:地理编码响应

使用此类获取位置的 JSONObject:

public class GetLocationDownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {


        String result = "";
        URL url;
        HttpURLConnection urlConnection;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);

            int data = inputStreamReader.read();
            while(data != -1){
                char curr = (char) data;
                result += curr;
                data = inputStreamReader.read();
            }
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if(result != null) {
            try {
                JSONObject locationObject = new JSONObject(result);
                JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

您可以像这样创建并运行类实例:

String link = "https://maps.googleapis.com/maps/api/geocode/json?address=" + addressString + "&key={YOUR_API_KEY}";

GetLocationDownloadTask getLocation = new GetLocationDownloadTask();

getLocation.execute(link);

Had the same problem. Pooling didn't work so I used this to get the location: Geocoding Responses

Use this class to get JSONObject of location:

public class GetLocationDownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {


        String result = "";
        URL url;
        HttpURLConnection urlConnection;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);

            int data = inputStreamReader.read();
            while(data != -1){
                char curr = (char) data;
                result += curr;
                data = inputStreamReader.read();
            }
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if(result != null) {
            try {
                JSONObject locationObject = new JSONObject(result);
                JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

You can create and run the class instance like this:

String link = "https://maps.googleapis.com/maps/api/geocode/json?address=" + addressString + "&key={YOUR_API_KEY}";

GetLocationDownloadTask getLocation = new GetLocationDownloadTask();

getLocation.execute(link);
落叶缤纷 2024-10-16 19:03:54

在 Android 2.0 上运行你的代码,它会工作的。我对可能的代码也有同样的问题。 2.2 不行,不知道为什么

Run your code on Android 2.0, It will work. I had the same problem with may code. Its not working in 2.2, don't know why

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文