0

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");
           }
geocodezip
  • 158,664
  • 13
  • 220
  • 245
flaviumanica
  • 195
  • 1
  • 4
  • 14

1 Answers1

0

You are calling .getDetails on each result returned. That method is subject to a quota and a rate limit. If you exceed the rate limit, the service returns a status of OVER_QUERY_LIMIT, which your current code is ignoring.

proof of concept fiddle

To address the issue, you need to slow your requests to the details service down to comply with the rate limit. Related question: OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?

code snippet:

function initMap() {
  var lng;
  var lat;
  var my_loc = new google.maps.LatLng(37.4419, -122.1419);
  map = new google.maps.Map(document.getElementById('map'), {
    center: my_loc,
    zoom: 10
  });
  geocoder = new google.maps.Geocoder;
  infowindow = new google.maps.InfoWindow();
  service = new google.maps.places.PlacesService(map);
  service.nearbySearch({
    location: my_loc,
    radius: 10000,
    type: ['store']
  }, callback);
}

function callback(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    console.log("nearbySearch returned " + results.length + " results")
    results.map(function(item) {
      var id = item.place_id;
      service.getDetails({
        placeId: id
      }, function(place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          createMarker(item);
        } else console.log("error: status=" + status);
      });
    });
  }
}

function createMarker(place) {
  console.log("adding place " + place.name + " loc=" + place.geometry.location.toUrlValue(6));
  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", initMap);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • I see. But why does it work then when an alert("smth") is added? And is there any way I can make it work without that alert? – flaviumanica Mar 07 '17 at 18:13
  • It works with the `alert` because that slows down the requests to less than the rate limit. To address the issue, you need to make your requests to the details service comply with the rate limit. Related question: [OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?](http://stackoverflow.com/questions/11792916/over-query-limit-in-google-maps-api-v3-how-do-i-pause-delay-in-javascript-to-sl) – geocodezip Mar 07 '17 at 18:33