Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps geocoding and markers in loop

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 location on a map.

But for some reason only one marker appears. I guess this has to do with the closures problem I've seen in other threads around here but I just can't seem to apply the solution to what I have.

My code is as follows:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  map.fitBounds(bounds);

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

function addMarker(map, item, location) {
  var marker = new google.maps.Marker({ map : map, position : location});
  marker.setTitle(item.title);
  var infowindow = new google.maps.InfoWindow( {
    content : item.body,
    size : new google.maps.Size(100, 300)
  });
  (function(map, marker) {
    new google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
    })(map, marker);
  }

Any help is appreciated.

Update: To avoid closures in loops as suggested in first answer, I have changed the code to this:

//This is the entry
function codeLocations(list, map) {
  for (var i = 0; i < list.length; i++) {
    console.log("Looping " + list[i].location);
    var geocoder = new google.maps.Geocoder();
    var geoOptions = {
      address: list[i].location,
      bounds: getBounds(),
      region: "NO"
    };
    geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
  }
}

function createGeocodeCallback(item, map) {
  console.log("Generating geocode callback for " + item.location);
  return function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      console.log("Geocoding " + item.location + " OK");
      addMarker(map, item, results[0].geometry.location);
    } else {
      console.log("Geocode failed " + status);
    }
  }
}

function addMarker(map, item, location) {
  console.log("Setting marker for " + item.location + " (location: " + location + ")");
  var marker = new google.maps.Marker({ map : map, position : location});
  marker.setTitle(item.title);
  var infowindow = new google.maps.InfoWindow( {
    content : item.body,
    size : new google.maps.Size(100, 300)
  });
  new google.maps.event.addListener(marker, "click", function() {
    infowindow.open(map, marker);
  });
}

According to the log statements, I now have the correct objects at the correct places, meaning the item and location objects are different for every time the marker is set, but I still only get one marker on my map. How can this be?

like image 432
fiskeben Avatar asked Nov 06 '10 11:11

fiskeben


1 Answers

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());
  }
like image 61
Harmen Avatar answered Sep 19 '22 19:09

Harmen