Google Maps geocoding and markers in loop

后端 未结 1 613
Happy的楠姐
Happy的楠姐 2021-02-04 18:19

I\'m completely puzzled here. I have a list of objects each containing a location. I look up this location using the google.maps.geocoder and afterwards I put a marker for that

1条回答
  •  温柔的废话
    2021-02-04 19:22

    Don't create closures in loops. That just won't work. This might be a solution for the problem:

      function callback() {
        return function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            addMarker(map, item, results[0].geometry.location);
          } else {
            console.log("Geocode failed " + status);
          }
        };
      }
    
      for (var item in list) {
        var geocoder = new google.maps.Geocoder();
        var geoOptions = {
          address: item.location,
          bounds: bounds,
          region: "NO"
        };
        geocoder.geocode(geoOptions, callback());
      }
    

    0 讨论(0)
提交回复
热议问题