2016-04-10 40 views
0

在gmap3谷歌地圖庫中,我正在試圖製作一個函數,用infowindow製作一個標記。在gmap3中檢索錯誤的標記

http://gmap3.net/api-infowindow.html

function addMarker(map, marker, content) { 
    map.marker(marker) 
    .infowindow({ 
     'content' : content 
    }) 
    .then(function (infowindow) { 
     var map = this.get(0); 
     var marker = this.get(1); // <---- this gets the first marker on both times I call addMarker, i.e. uluru 
     marker.addListener('click', function(event, data) { 
      infowindow.open(map, this); 
     }); 
    }); 
} 

$(document).ready(function() { 

    var uluru = {lat: -25.363, lng: 131.044}; 

    var map = $('#map') 
     .gmap3({ 
     zoom: 4, 
     center: uluru 
     }); 

    addMarker(map, { 
     position: uluru 
    }, "text"); 
    addMarker(map, { 
     position: {lat: 48.8620722, lng: 2.352047} 
    }, "text2"); 
}); 

這是我有什麼,但問題是,在頂部,在那裏我試圖得到一個標記(我把代碼中的註釋),這似乎是引用錯誤的標記。這兩次我都稱之爲,它引用了我製作的第一個標記。因此,如果我點擊地圖上的第一個標記,則會同時顯示該標記上的infowindows。

有誰知道最新錯誤?

感謝

回答

0

「獲取」功能檢索從一開始的鏈接結果,指數永遠不會改變。利用當地

存儲在你的功能「然後」

function addMarker(map, marker, content) { 
    var gmMarker; 
    map 
     .marker(marker) 
     .then(function (m) { 
     gmMarker = m; 
     }) 
     .infowindow({ 
     'content' : content 
     }) 
     .then(function (infowindow) { 
     var map = this.get(0); 
     gmMarker.addListener('click', function(event, data) { 
      infowindow.open(map, this); 
     }); 
     }); 
}