0

I am trying to add an info window when the user clicks on the marker on the map. When I click the marker to test, I am getting this error: Uncaught TypeError: Cannot read property '__e3_' of undefined. Here is my code:

for(var i = 0; i < final_search_results.length; ++i) {
    var title = ("Parameter: " + final_search_results[i].parm_desc + "; Date: " + final_search_results[i].date + "; Result: " + final_search_results[i].result);
    var markerLatlng = new google.maps.LatLng(final_search_results[i].st_id.lat_coord, final_search_results[i].st_id.long_coord); // Add the coordinates
    var marker_icon = new google.maps.MarkerImage("./marker_icon.png", null, null, null, new google.maps.Size(64,64))
    var marker = new google.maps.Marker({
        position: markerLatlng,
        icon: marker_icon,
        map: map,                                   
        title: title
    });

    marker['result'] = final_search_results[i].result;
    marker['date'] = final_search_results[i].date;
    marker['time'] = final_search_results[i].time;
    marker['station'] = final_search_results[i].st_id.station_id;

    marker_array.push(marker);
}

for(var k = 0; k < marker_array.length; ++k) {
    var content = ("<p>Station: " + marker_array[k].station + "; Date: " + marker_array[k].date + "</p>");
    var infowindow = new google.maps.InfoWindow({ // Create a new InfoWindow
        content: content// HTML contents of the InfoWindow
    );

    google.maps.event.addListener(marker[k], 'click', function() { // Add a Click Listener to our marker
        infowindow.open(map,marker[k]); // Open our InfoWindow
    });  
}           

I only get this error when I click the marker. Everything was fine before I added the code blow the *. If you would like to see more code just let me know.

user3210944
  • 57
  • 1
  • 6
  • 1
    possible duplicate of [Google Maps JS API v3 - Simple Multiple Marker Example](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example). k is greater than marker_array.length after the loop exits. – geocodezip Jan 20 '14 at 18:49
  • @geocodezip forgive me if this is a stupid qusetion but how can k be greater when I put `k < marker_array.length` as one of the conditions in the `for-loop`? – user3210944 Jan 20 '14 at 19:18
  • Sorry, I should have been clearer. The loop exists when k = marker_array.length. k=0 is the first element, k=marker_array.length is not a valid element of the array (the last valid element is marker_array[marker_array.length-1]. – geocodezip Jan 20 '14 at 19:22
  • @geocodezip True. Correct me if I'm wrong but does't `marker_array[marker_array.length-1] == But I put `<` meaning if the array has 5 indexes k will only increase until it is 4 (`k < marker_array.length`)... or am I not getting something? – user3210944 Jan 20 '14 at 22:38
  • Step through the loop in a debugger. – geocodezip Jan 20 '14 at 23:05
  • @user3210944 - when the loop finishes, k will be 5. It will loop 0..4, then exits the loop as soon as it equals the array length, i.e. is OVER the limit you've specified to loop until. So then your event listener is specifying `infowindow.open(map,marker[k]);` where k = 5, i.e. doesn't exist in your array. Instead you need to use a closure to pass in the value of k when you create your event listener, otherwise it will always use whatever the last value of k was, and in this case the real problem is you'd end up just opening the infowindow always anchored to the last marker! – duncan Jan 21 '14 at 09:31

0 Answers0