I want to introduce a functionality which allows a marker’s infoname to appear or disappear upon mouseover or mouseout of a corresponding DIV element generated from jQuery. However, I am getting a “a is undefined” error on line 19 of main.js. After extensive testing on my script, I realise that this has something to do with the marker in the newly added lines as commented below:
function addMarker(A) {
var point = new google.maps.LatLng(A.lat, A.lng);
var image = new google.maps.MarkerImage('images/r.png',
new google.maps.Size(30, 30),
new google.maps.Point(0, 0),
new google.maps.Point(0, 30));
marker = new google.maps.Marker({
map: map,
position: point,
icon: image,
});
}
function addInfoName(A) {
var infoname = new infoName; // custom object
google.maps.event.addListener(marker, 'mouseover', function(event) {infoname.show();});
google.maps.event.addListener(marker, 'mouseout', function(event) {infoname.hide();});
infoname.open(map, marker);
}
function showResult(A) {
$('#results').append('<DIV id=' + A.pid + '>{Blah Blah Blah}</DIV>');
return document.getElementById(A.pid);
}
function process(json) {
$('#results').empty();
total = json.details.length;
for(i=0; i<total; i++) {
var detail = json.details[i];
var marker;
addMarker(detail);
addInfoName(detail);
// these new lines are added
var listDisplay = showResult(detail);
listDisplay.onmouseover = function(){google.maps.event.trigger(marker, 'mouseover');};
listDisplay.onmouseout = function(){google.maps.event.trigger(marker, 'mouseout');};
}
}
google.maps.event.addListener(map, 'idle', function () {$.getJSON(query, process);});
The error disappears if I merge the function addInfoName into process. However, all the DIVs will point to the last marker if I do so. My question is, how do I modify my script to achieve the functionality mentioned above?
Currently you’ve got a variable
markerdeclared local to theprocessfunction, but you’re trying to read and write to it from other functions. In particular,addMarkerwrites tomarkerwithoutvar, which causes an accidental global variable to be created. Meanwhileprocessis not actually writing to themarkerlocal it has declared, so it containsundefined, which will trip the Google Maps code up when you pass that in.(Tools like jslint, or ECMAScript 5 Strict Mode can catch accidental globals for you. Note
totalandiare also accidental globals.)It looks like
addMarkerandaddInfonamehave been hacked out of the body ofprocesswithout tying up the variables fromprocessthat both of them used. If they were included in the body ofprocessit would work, but you’d get the described behaviour where the samemarkervalue was used for every div because of the Closure Loop Problem.This problem occurs in languages with closures and function-level scope, which includes JavaScript, Python and others. In these languages any variables defined by or inside a
forloop are local to the containing function, not reallocated every time you go around the loop. So if you make a closure referring toiin the first iteration of the loop, it’s the same variableias you refer to in the second iteration of the loop; every instance of the function has a closure over the same variableiso every function will see the same value. The same would be true ofmarker.The Closure Loop Problem can be avoided through use of a second closure that keeps the loop variable in an argument, or, more cleanly, using a closure-based loop mechanism instead of the C-like
forloop. ECMAScript 5 offersarray.forEach()for this purpose and jQuery offers$.each():