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

时间:2014-08-31 06:42:56

标签: javascript google-maps

我有implemented a sample using Places Search Google Maps API。我想获取我正在获取的结果数。 Google Maps API新手。我如何实现这一目标?

jsFiddle

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);

1 个答案:

答案 0 :(得分:1)

用这个替换你的回调:

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]);
    }
  }
}