Google Maps API V3- Adding listener on markers after they are created
I got a problem. I am making a map where you can add markers, polylines and polygons using "tools". Like MS Paint and tools there. You click on tool, and then do whatever you want on map.
The thing is, I am adding markers without any listener开发者_StackOverflow中文版s, so I can't click them - because I want that tool just to put marker in chosen place.
And my problem is to create a tool, which would let me to delete these markers. But to do that, my tool would need to add click event listener to markers.. and they were created some time ago. Is that possible? Because when I try just to make addeventlistener (marker, 'click, xxx()) i get the error that marker is not defined.
I wont post whole code here, because its a lot of it, but if you need any part, just ask, I will post it ASAP.
EDIT1:
As Majid Fouladpour said, global variable works but only for last added marker :o?
You need to make marker
a global variable. You are defining it inside a function now:
function somefunction(params) {
...
var marker = ....
...
}
Instead, you should have:
var marker;
...
function somefunction(params) {
...
marker = ....
...
}
UPDATE
Your comment shows you are now getting the reference part right. If your function is not fired, you should be having an issue in your add listener part of the code. Are you doing it this way?
google.maps.event.addListener(marker, 'click', function() {
alert('x');
});
精彩评论