javascript - Google Places API 在查询时是否可以返回一个对象而不是一组对象?

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

我正在尝试使用 Google Places API,我想知道是否可以只返回一个最接近的结果。在下面的示例中,我可以返回 1 公里半径内的所有健身房,这很好,但如果我要返回最近的警察局或医院,而我只想知道最近的警察局或医院,有没有办法做到这一点。看来 API 正在返回半径内的所有对象,并且无法更改。我似乎找不到任何强调此问题的文档,并且我所做的任何尝试仍然返回该区域中的所有地点。

 function GymReport(){

  // Gets the latitude and longitude of a location searched by a user
  $('.search_latitude').val(marker.getPosition().lat());
  $('.search_longitude').val(marker.getPosition().lng());

 var Lat = marker.getPosition().lat();
 console.log(Lat);

 var Long = marker.getPosition().lng();
 console.log(Long);

 var location = {lat: Lat, lng: Long};

   var service = new google.maps.places.PlacesService(map);
   service.nearbySearch({
       location: location,
       radius: 1000,
       type: ['gym']
     }, callback);
    }

回调类

 function callback(results, status) {

  if (status === google.maps.places.PlacesServiceStatus.OK) {
      if(marker)
            marker.setMap(null)
     for (var i = 0; i < results.length; i++) { 
          createMarker(results[i]);<-- This calls the function that will create the markers for the array of results from the API.
        }
  }
}

最佳答案

请注意,您可以按知名度或按距离对附近地点搜索结果进行排序。默认情况下,它是按重要性排序的。您可以在代码中使用 rankBy 参数按距离排序:

rankBy - Specifies the ranking method to use when returning results. Defaults to PROMINENCE. Note that when rankBy is set to DISTANCE, you must specify a location but you cannot specify a radius or bounds.

来源:https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlaceSearchRequest

一旦获得按距离排序的结果,只需从数组中获取最近位置的第一个元素即可。看看我的示例代码

var map;
var infowindow;

function initMap() {
  var pyrmont = {lat: -33.867, lng: 151.195};

  map = new google.maps.Map(document.getElementById('map'), {
    center: pyrmont,
    zoom: 15
  });

  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: pyrmont,
    rankBy: google.maps.places.RankBy.DISTANCE,
    type: ['gym']
  }, callback);
}

function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    //Get the first result, it's the closest one
    createMarker(results[0]);
  }
}

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);
  });
}
#map {
  height: 100%;
}
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initMap" async defer></script>

希望这会有所帮助!

关于javascript - Google Places API 在查询时是否可以返回一个对象而不是一组对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48752769/

相关文章:

javascript - 添加具有输入值的行

javascript - 在 Angular 2 Google map 上移动 map 控件

javascript - 谷歌地图 : How to avoid google map usage limit of 25000 maps load per day

javascript - CSS 溢出-y : scroll overwrite my code and other functions not working

javascript - PHP - 如果文件存在则运行 JavaScript

Javascript 区分组合与继承

ios - GooglePlacePicker 不通过 cocoa pod 安装

javascript - 让谷歌地图回调等待其余功能完成

javascript - 在 Google map 上制作漂亮的标签

javascript - 如何从 Google map 中删除多边形