2016-09-28 75 views
-1

我使用JQuery google map api並在我的地圖上放置了一些製作者。 這就好比如何從googlemap中提取id addmarker()

$("#map").googleMap(); 
$("#map").addMarker({ 
    coords: [48.895651, 2.290569], // GPS coords 
    url: 'http://www.tiloweb.com', // Link to redirect onclick (optional) 
    id: 'marker1' // Unique ID for your marker 
}).addClass("clickit"); 

代碼,現在,我遇到一個問題,即如何獲得ID:「MARKER1」在addMarker()方法時,我想用onclick事件這樣

$(".clickit").on('click', function(event) { 
    //Get the id: 'marker1' here 
    }); 
+0

顯示您的addMarker和googleMap函數 – scaisEdge

+0

我使用CDN

回答

1

嘗試存儲標記在一個全局變量:

//global variable: 
var = markers[]; 

//code: 
$("#map").googleMap(); 

//declare a local variable to store markers: 
var marker = $("#map").addMarker({ 
    coords: [48.895651, 2.290569], // GPS coords 
    url: 'http://www.tiloweb.com', // Link to redirect onclick (optional) 
    id: 'marker1' // Unique ID for your marker 
}).addClass("clickit"); 

//store local in a global variable: 
markers.push(marker); 

隨後,只聽事件:

$(".clickit").on('click', function(event) { 
    console.log(marker.id); 

    });