我在谷歌地图上有多个标记,当我单击它们时,我需要打开一个模式窗口。问题是每个标记都有一个特定的 id,我需要在每个模式中使用该 id。
模态 html 是:
<div class="modal fade" id="imageModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
调用该函数的 JavaScript 代码是:
function createMarker(pos, t, map) {
var marker = new google.maps.Marker({
position: pos,
map: map, // google.maps.Map
index: t
});
google.maps.event.addListener(marker, 'click', function() {
$('#imageModal').modal();
//alert("I am marker " + marker.index);
});
return marker;
}
所以在imageModal中我需要访问动态的marker.index信息。我怎样才能做到这一点?有什么想法吗?
最佳答案
首先,我不认为 index
是 MarkerOption在 Google map v3 中。
我是如何得到这个的,我在 MarkerOption 中使用了 title
选项,并将其设置为动态字符串,然后在 addListener
事件中访问它。
具体操作方法如下:
function createMarker(pos, t, map) {
var marker = new google.maps.Marker({
position: pos,
map: map, // google.maps.Map
title: t //where t must be a string
});
google.maps.event.addListener(marker, 'click', function() {
$('#imageModal').modal();
alert("I am marker " + this.getTitle() );
});
}
希望这对你有帮助!!
关于javascript - 将数据传递给模态( Bootstrap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18694541/