谷歌地图地理代码-结果相关性

Google Maps Geocode - Results relevance

本文关键字:结果 相关性 代码 谷歌地图      更新时间:2023-09-26

我正在使用Google Maps Javascript API的基本功能

来自google的代码示例: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
因此,正如您所看到的,当我们输入街道名称时,即使我们有几个结果,上面的示例也只显示一个。我已经将其更改为循环遍历所有结果,它工作得很好。

我的问题是:

geocoder.geocode 结果按任何标准排序?

如果答案是肯定的,是否有可能根据当前用户位置的最相关/最近位置进行排序?(我将传递当前用户位置)。

Thanks to lot

您使用的API是错误的,请尝试使用Google Places API。按流行程度排序(首选)。

或者您可以使用一些代码来遍历结果并查找到原始位置的距离。然后你可以按距离排序(可以,但我认为流行度是你想要的)

我知道最接近的是使用区域偏置:https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes