javascript - Maps Marker click targets wrong marker -


So I make several Google Maps markers through a loop and add the click listener, to open the related information Window is considered for every marker click always opens the info window of the marker, which was added last time.

  Private loader (): void {net.Ajax.getJson ("/ places.json", (Response: Location []) = & gt; {for (var i = 0; i & lt; response.length; i ++) {var location: location = response [i]; var marker: google.maps.Marker = this.createMarker (location); Google.maps.event.addListener (marker, "click ', () => {This.onMarkerClicked (Marker);});}}); } Marker-Linked on Private (Marker: google.maps.Marker): Zero {this.infoWindow.close (); This.infoWindow = New google.maps.InfoWindow ({content: marker.getTitle ()}); This.infoWindow.open (this.map, marker); }  

Any ideas? Thanks!

This classic closed conceptual mistake is. Closer does not consider variable marker and whenever the variable has the scope of function , you have only one marker .

GetJson ("/ places.json", (Response: Location []) = & gt; {for (var i = 0; i & lt; response.length; i ++) {(function () {var location: location = response [i]; var marker: google.maps.Marker = this.createMarker (location); google.maps.event.addListener (marker, 'click', () = & gt; {This.onMarkerClicked (marker); })}}) ();}});

PS: I have a video on this topic:


Comments

Popular posts from this blog

eclipse plugin - Run java code error: Workspace is closed -

ios - How do I use CFArrayRef in Swift? -

scala - Play Framework - how to bind form to a session field -