java - 安卓地理编码器 : search by zip code returns unexpected results

标签 java android google-maps

我想通过 Android 的 GeoCoder 类搜索用户输入的文本。这是我的代码:

Geocoder iGeocoder = new Geocoder(getContext(), Locale.GERMAN);

public Address getAdress(String aUserInput) {
    List<Address> tAddressList = iGeocoder.getFromLocationName(aUserInput, 1000, 47.060940, 8.564278, 51.526396, 13.736392);
    if(tAddressList != null &&
                tAddressList.size() > 0) {
        for(Address tAddress : tAddressList) {

            // return the first adress found for germany.
            Log.e(TAG, "returning Adress: " + tAddress );
            if(tAddress.getCountryCode().equals("DE")) {

                return tAddress;
            }

        }
        return null;
    } else {
        return null;
    }
}

我传递给 getFromLocationName 的边界框大致代表德国州 Bayern。我知道这个边界框不能保证结果地址确实存在。我发现了一些问题,尤其是在通过邮政编码搜索时。

  • 按邮政编码搜索时返回的一些结果地址没有“管理区域”,因此我无法过滤我想要的州 (="Bayern")。
  • 奇怪的是,如果我搜索邮政编码 97070、97078 或 97076,GeoCoder 只会返回美国俄勒冈州的地址。然而,这些邮政编码也位于德国小镇维尔茨堡。例如。 97082 将返回 Würzburg 地址,但是找不到 97070(Würzburg central),它只返回俄勒冈州的地址。

最佳答案

尝试自定义您的 locale .使用 GERMANY 和 GERMAN。 Germany用于德国的语言环境,而不是 language .

Locale locale = new Locale(langCode, countryCode) 

Locale locale = new Locale("de", "DE");

Geocoder iGeocoder = new Geocoder(getContext(), locale);

GeoCode 默认为美国,因此它似乎首先在搜索邮政编码时进行过滤。如果你test here :

97070 给出了 Wilsonville, OR 97070, USA 的结果

47.060940, 8.564278 给出 Spitzibüelstrasse 8, 6410 Goldau, Switzerland

97070, 47.060940, 8.564278 没有给出任何结果(这只是测试者),它无法区分结果以包含邮政编码和经纬度。

如果您需要增加语言环境区域 - 您始终可以创建一个包含 3 个国家/地区语言环境的数组并在每个语言环境上进行搜索。

你也有太多的返回 - 你只需要在第一个 if 之后返回一个 null 并且如果它没有返回地址,以确保所有路径都有一个返回值。

public Address getAdress(String aUserInput) {
    .../...
    if(.../...) {
        for(Address tAddress : tAddressList) {
            .../...
            if(tAddress.getCountryCode().equals("DE")) {

                return tAddress;
            }
        }
     }
    return null;
}

还有 Region Biasing 感谢jausen brett对于链接

https://maps.googleapis.com/maps/api/geocode/json?address=<some address>&region=de&key=YOUR_API_KEY

Component Filtering

https://maps.googleapis.com/maps/api/geocode/json?components=locality:<desired locality>|country:DE&key=YOUR_API_KEY

关于java - 安卓地理编码器 : search by zip code returns unexpected results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631673/

相关文章:

java - 如何正确加密用 jackson 编写的 JSON 文件

java - 使 "class"成为 transient 或可序列化但该类是可序列化的

android - 如何在 DatePickerDialog Android 中禁用今天日期之后的日期?

javascript - 使用 Google Earth API 飞到 PlaceResult

google-maps - 谷歌地图中信息窗口的最大宽度?

php - 使用 PHP 解析 KML 文件

JavaFX ObjectBinding不会触发computeValue,绑定(bind)属性不会改变

java - 确定网络连接带宽(速度) wifi 和移动数据

Android 7 中的 Androidx 导航图标兼容性问题

android - 使用 webview 播放 youtube 视频时出现问题