谷歌地图Api附近搜索-为什么Google.Maps.places.RankBy.DISTANCE返回的结果更少

Google Maps Api Nearby Search - Why does google.maps.places.RankBy.DISTANCE return fewer results?

本文关键字:返回 DISTANCE 结果 RankBy places 附近搜索 Api 为什么 Maps Google 谷歌地图      更新时间:2023-09-26

正如标题所述。我尝试了google.maps.places.RankBy.PROMINENCE(默认值)并将半径设置为50000,返回了20个结果。但是,当我尝试完全相同的搜索,减去文档中的半径,并使用google.maps.places.RankBy.DISTANCE时,在我所在位置的短半径内只返回3个结果。有人能解释为什么会发生这种情况,以及如何在附近进行远距离搜索并获得完整结果吗。也许我只是做错了什么。谢谢

以下代码使用google.maps.places.RankBy.PROMINENCE 返回20个结果

var map;
var infowindow;
function initMap() {
  var location = {lat: 43.139387, lng: -80.264425};
  map = new google.maps.Map(document.getElementById('map'), {
    center: location,
    zoom: 9
  });
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: location,
    radius: 50000,
    types: ['police'],
    // rankBy: google.maps.places.RankBy.DISTANCE
  }, callback);
}
function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}
function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}
<!DOCTYPE html>
<html>
  <head>
    <title>Place searches</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&signed_in=true&libraries=places&callback=initMap" async defer></script>
  </body>
</html>

使用google.maps.places.RankBy.DISTANCE 时,以下代码仅返回3个结果

var map;
var infowindow;
function initMap() {
  var location = {lat: 43.139387, lng: -80.264425};
  map = new google.maps.Map(document.getElementById('map'), {
    center: location,
    zoom: 9
  });
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: location,
    // radius: 50000,
    types: ['police'],
    rankBy: google.maps.places.RankBy.DISTANCE
  }, callback);
}
function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}
function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}
<!DOCTYPE html>
<html>
  <head>
    <title>Place searches</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&signed_in=true&libraries=places&callback=initMap" async defer></script>
  </body>
</html>

PROMINENCE代码的radius属性可能会对搜索结果产生影响。根据Places文档,它可能会受到谷歌指数、全球知名度等的影响。

对于DISTANCE,现在需要keywordnametypes等可选属性。

希望这能澄清你的问题。