[英]How do I use function argument to find exact clicked marker in google maps?
我的標記將始終在單擊它時顯示最后的信息。 基本上,我想為每個單擊的標記顯示相應的信息。 這是我到目前為止所擁有的:
$(function() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom : 2,
center: new google.maps.LatLng(43.65654, -79.90138),
mapTypeControl: false, //will remove the top right corner Map type(Satellite/Map)
//scaleControl: false,
navigationControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
$.get('example.xml', function(xml) {
$('marker',xml).each(function(i) {
var $this = $(this),
the_marker = new google.maps.Marker({
title: $this.find('label').text(),
map: map,
clickable: true,
position: new google.maps.LatLng(
parseFloat($this.find('lat').text()),
parseFloat($this.find('lon').text())
)});
new google.maps.event.addListener(the_marker, 'click', function() {
getInfo();
});
});
});
function getInfo() {
$.ajax({
type:'GET',
url:'example.xml',
dataType:'xml',
success: function(xml) {
$(xml).each(function() {
$(this).find("marker").each(function() {
$(this).find("info");
$("#newDiv").html($(this).find("info"));
})
})
}
})
$("#newDiv").html($(this).find("info").text());
}
});
就像我總是在單擊標記時獲得最后一個信息窗口一樣,但是我希望每個標記都具有自己的信息窗口。 現在,我的示例中沒有infowindow,而其中有div。
謝謝
您可以簡單地使用數組來做到這一點。
喜歡,
markers[i] = new Marker({ some options });
new google.maps.event.addListener(markers[i], 'click', function() {
getInfo(i);
});
添加:
仍然無法理解為什么您想在單擊任何標記時僅在xml中獲得第一個標記。
任何演示或示例?
如果您想獲取點擊標記的信息,請按照以下步驟進行操作。
markers[i] = new Marker({ some options });
info[i] = $(this).find('info');
new google.maps.event.addListener(markers[i], 'click', function() {
$("#newDiv").html(info[i]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.