简体   繁体   English

Google Maps API地方搜索获取搜索结果数

[英]Google Maps API Places Search get number of search results

I have implemented a sample using Places Search of Google Maps API. 我已经使用 Google Maps API的Places Search实现了一个示例 I would like to fetch the number of results I am fetching. 我想获取我正在获取的结果数。 New to Google Maps API. Google Maps API的新功能。 How do I achieve this? 我该如何实现?

jsFiddle 的jsfiddle

JS: JS:

var map;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(19.107567, 72.8335);

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

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['hospital']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, 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);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

Replace your callback with this: 将此替换为回调:

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    // Just count the number of results passed to your callback
    var numResults = results.length;

    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps Places API-如何在多个结果上使用Google Maps搜索框获取国家/地区,邮政编码,数字,网站URL - Google Maps places API - How to get Country, Postal-code, Number, Website URL with Google map search box on multiple results 如何在Google Maps API搜索结果中显示自定义地点? - How to display custom places in Google Maps API search results? Google Maps Api附近搜索-为什么google.maps.places.RankBy.DISTANCE返回的结果更少? - Google Maps Api Nearby Search - Why does google.maps.places.RankBy.DISTANCE return fewer results? 带有地理位置和地点搜索功能的Google Maps API - Google Maps API with Geolocation and Places Search Javascript,填充谷歌地图结果将API搜索结果放入下拉列表框 - Javascript, populate the result of Google maps places API search results into Dropdown list box 估计搜索结果数Google搜索API - Estimated number of search results Google search API 在 Google Maps Places API 中限制基于特定国家/地区的搜索 - Restricting search based on a particular country in the Google Maps Places API Google Maps API搜索 - Google Maps API Search 搜索不带地点的Google Maps Box - Search Google Maps Box without Places Google Maps Places自动完成搜索任何位置 - Google Maps Places Autocomplete search any position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM