谷歌地图Api附近搜索 - 为什么google.maps.places.RankBy.DISTANCE返回的结果较少?

时间:2015-12-22 23:05:33

标签: javascript google-maps google-maps-api-3

正如标题所述。我尝试了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>

1 个答案:

答案 0 :(得分:0)

PROMINENCE代码的radius属性可能会对搜索结果产生影响。根据{{​​3}},它可能会受到谷歌索引,全球知名度等的影响。

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

希望这澄清了有关您的问题的一些观点。