Reputation: 1534
I try to add a function on every map marker click. So every map marker click do another function. Here is my function:
function test1() {
jQuery('#Nav1').toggle();
}
function test2() {
jQuery('#Nav2').toggle();
}
Here are my markers:
var markers = [
{ lat: 43.963395, lng: 15.419338, name: "Marker1", icon:'http://maps.google.com/mapfiles/ms/icons/red-dot.png', funkcija: "test1" },
{ lat: 45.001831, lng: 14.89686, name: "Marker2", icon:'http://maps.google.com/mapfiles/ms/icons/red-dot.png', funkcija: "test2" }
];
And here is my function to create markers:
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
// Create the marker
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.lng),
map: map,
title: data.name,
icon: data.icon
});
google.maps.event.addListener(marker, "click", function() {
// do the function
});
I try with this but nothing.
google.maps.event.addListener(marker, "click", function() {
data.funkcija()
});
If I manualy enter function then it works but same fonction for all markers
google.maps.event.addListener(marker, "click", function() {
test1()
});
Any idea?
Upvotes: 1
Views: 2849
Reputation: 31930
What you can do is rely on the functions being part of the windows scope, and refer to them dynamically like so:
google.maps.event.addListener(marker, "click", function() {
window[data.funkcija]();
});
See http://derek.io/blog/2009/dynamic-function-names-in-javascript/
Upvotes: 2