I have a little problem regarding JavaScript with Google Maps API. I need to find all the stores near a certain place, so I have a function that contains this:
service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: my_loc,
radius: 500,
type: ['store']
}, callback);
The callback function looks like this:
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
results.map(function (item) {
// do things here
});
}
}
The problem is that the results array has 16 elements (if I alert(results.length) in callback, it shows the value 16, which is right), however the mapping over results only takes into account 10 of them. What is strange is that if I add "alert(item)" in the mapping function, then it will take into account all the 16 elements and do the right stuff with them.
Why does this alert make the function see the whole array? And how can I make it see all the elements without that alert? Thanks!
UPDATE: Full code
function initMap() {
var lng;
var lat;
navigator.geolocation.getCurrentPosition(function (position) {
var my_loc = { lat: position.coords.latitude, lng: position.coords.longitude };
map = new google.maps.Map(document.getElementById('map'), {
center: my_loc,
zoom: 15
});
geocoder = new google.maps.Geocoder;
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: my_loc,
radius: 500,
type: ['store']
}, callback);
}, handle_errors);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
results.map(function (item) {
var id = item.place_id;
service.getDetails({ placeId: id }, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var lista = document.getElementById("lista");
var elem = document.createElement("tr");
var text1 = document.createTextNode(place.name + " ");
var sp1 = document.createElement("td");
sp1.appendChild(text1);
elem.appendChild(sp1);
lista.appendChild(elem);
createMarker(item);
}
});
});
}
}
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);
});
}
function handle_errors(error) {
alert("Geolocation is not enabled");
}