基于谷歌地理编码器中未知地址的建议

发布于 2024-08-27 21:47:51 字数 513 浏览 8 评论 0原文

当使用谷歌的地理编码器服务在地图上显示城市时;填写不存在的城市会导致错误。

有没有办法根据填写的城市显示一些建议?

var geocoder = new GClientGeocoder();
function showAddress(address, zoom) {

          geocoder.getLatLng(
           address,
           function(point) {
            if (!point) {
                  //no point found....
              //Suggest some points :)
            } else {
                map.setCenter(point, zoom);

            }
          }
         );
        }
showAddress('Someplace, Nederland', 14);

When using Googles geocoder service to display a city on a map; filling out a non-existing city results in an error.

Is there a way to display some suggestions based on the filled out city?

var geocoder = new GClientGeocoder();
function showAddress(address, zoom) {

          geocoder.getLatLng(
           address,
           function(point) {
            if (!point) {
                  //no point found....
              //Suggest some points :)
            } else {
                map.setCenter(point, zoom);

            }
          }
         );
        }
showAddress('Someplace, Nederland', 14);

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

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

发布评论

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

评论(3

无悔心 2024-09-03 21:47:51

建议在结果的 Placemarks 属性中返回。我正在使用 v3 并且这有效,请尝试使用 address="Washington Blvd" 来调用它

if (geocoder) {
                geocoder.getLocations(
                address,
                function (results) {
                    if (!results) {
                        alert(address + " no suggestions");
                    } else {
                        $('#output').html('');
                        if (results.Placemark) {
                            for (var i = 0; i < results.Placemark.length; i++) {
                                $('#output').append(i + ': ' + results.Placemark[i].address + '<br/>');
                            }
                        }
                    }
                });
            }

Suggestions are returned in the Placemarks attribute of the results. I'm using v3 and this works, try calling this with address="Washington Blvd"

if (geocoder) {
                geocoder.getLocations(
                address,
                function (results) {
                    if (!results) {
                        alert(address + " no suggestions");
                    } else {
                        $('#output').html('');
                        if (results.Placemark) {
                            for (var i = 0; i < results.Placemark.length; i++) {
                                $('#output').append(i + ': ' + results.Placemark[i].address + '<br/>');
                            }
                        }
                    }
                });
            }
心是晴朗的。 2024-09-03 21:47:51

如果您只是对城市进行地理编码,您可能需要考虑开始构建自己的城市到坐标缓存。

这是您可能需要考虑的一种方法:

  • 提示用户输入城市名称。
  • 首先向您的服务器发出 AJAX 请求,以检查该城市名称是否存在于您的缓存中。
  • 如果是,则继续执行 JavaScript 逻辑,就像从 Google Geocoder 获取坐标一样。
  • 如果您的缓存中不存在城市名称,请向 Google 地理编码器发送请求,就像您当前所做的那样。
  • 如果 Google 地理编码器返回肯定结果,请向您的服务器发出另一个 AJAX 请求,以便将该城市及其坐标存储在您的缓存中。你永远不会再向谷歌询问这座城市的坐标。然后在您的 JavaScript 应用程序中正常进行。
  • 如果 Google Geocoder 返回“未知地址”,则告诉用户未找到该城市名称,并建议使用更常见的城市名称重试。记下尝试的原始城市名称。使用 Google 地理编码器测试第二次尝试,如果成功,请将城市名称的两个同义词保存到缓存中(第一次尝试,第二次成功,如果尚未存在)。

通过这种方法,您还可以播种缓存,以这种方式解析您已经知道 Google 无法进行地理编码的城市名称。

If you are simply geocoding cities, you may want to consider starting to build your own cities-to-coordinates cache.

This is one approach that you may want to consider:

  • Prompt the user to enter a city name.
  • First issue an AJAX request to your server to check if that city name is present in your cache.
  • If it is, proceed your JavaScript logic, as if you obtained the coordinates from the Google Geocoder.
  • If the city name was not present in your cache, send a request to the Google Geocoder, as you are currently doing.
  • If the Google Geocoder returns a positive result, issue another AJAX request to your server in order to store this city and its coordinates in your cache. You will never ask Google again for the coordinates of this city. Then proceed normally within your JavaScript application.
  • If the Google Geocoder returns "Unknown address" tell the user that this city name was not found, and suggest to retry using a more common city name. Keep note of the original city name that was attempted. Test the second attempt with the Google Geocoder, and if it succeeds, save into your cache both synonyms of the city name (the first one attempted, and the second on that succeeded if not already present).

With this approach you can also seed your cache, in such a way to resolve city names which you are already aware that Google is not able to geocode.

仲春光 2024-09-03 21:47:51

如果找到匹配项,它将返回匹配项作为地址,这些就是建议。它不应该返回错误,如果找不到匹配项,它应该只返回一个空列表。你能发布代码吗?

If it finds a match it will return the matches as Addresses and those are the suggestions. It shouldn't return an error, it should just return an empty list if it finds no matches. Can you post the code?

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