2014-08-31 230 views
0

我有implemented a sample using Places Search谷歌地圖API。我想獲取我提取的結果數量。 Google地圖API的新功能。我如何實現這一目標?Google Maps API Places搜索獲取搜索結果數量

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

本替換你的回調:

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