Javascript: the execution context of the function

0 like 0 dislike
Make a loop so that the markers Google maps hang listeneri show InfoWindow

for (var i = 0; i < points.length; i++) { var point = points[i]; var myLatLng = new google.maps.LatLng(, point.lng); var marker = new google.maps.Marker({ position: myLatLng, map: map, icon: point.icon title: visible:false }); google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(point.description); infowindow.setPosition(myLatLng);; }); point.marker = marker; }

As a result, when you click the marker it shows always to the last point.
I.e. the feeling that the function is not created in its context (as I used in Java), and uses the last value point of the series.

How it works in JavaScript? What to do to the listener for each marker showed the InfoWindow in the right place?

Thank you
by | 8 views

3 Answers

0 like 0 dislike
You have a short in point. You need to make add a listener to a function with one argument, which is to call in a loop and send back a pointer.
0 like 0 dislike
0 like 0 dislike
Inside the loop you need to put inside a function and call it:
(function() {
Then for each element of the cycle is its own environment.

Related questions

110,608 questions
257,186 answers
32,830 users