我正在使用谷歌构建交互式地图。我将有很多地图标记,当点击时将打开一个带有相对于该位置的内容的引导模式。是否可以在脚本中只进行一次模态调用,然后使用相对于单击标记的内容加载它?我以为我可以编写一个包含所有不同模态内容的远程html文件。但是现在,我必须为每个标记写一个点击功能(苦心经营),打开一个独特的模态。
1个模态,相对于单击的标记单击1次,根据单击的标记加载唯一的模态内容。可以这样做吗?
function initialize() {
var map;
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(43.819201,-79.535474),
disableUi: true
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var commerciallocations = [
['Regan',43.703264,-79.804144],
['Lake',43.897239,-78.6594789],
['Rowe',43.72277,-80.378554],
['Westport',43.649826,-79.6599653],
['Vinefresh',42.9556009,-81.6699305]
];
var marker2, i;
for (i = 0; i < commerciallocations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(commerciallocations[i][1], commerciallocations[i][2]),
map: map,
icon:'images/commercialmapicon.png'
});
if (i == [0]){
google.maps.event.addListener(marker, 'click', function() {
$('#modal0').modal();
});
}
if (i == [1]){
google.maps.event.addListener(marker, 'click', function() {
$('#modal1').modal();
});
}
if (i == [2]){
google.maps.event.addListener(marker, 'click', function() {
$('#modal2').modal();
});
}
if (i == [3]){
google.maps.event.addListener(marker, 'click', function() {
$('#modal3').modal();
});
}
if (i == [4]){
google.maps.event.addListener(marker, 'click', function() {
$('#modal4').modal();
});
}
};
我添加了脚本部分,我必须写,以告诉每个标记打开其各自的模态。看看我如何为数组的每个索引编写一个click事件。我不知道其他任何方式...
答案 0 :(得分:0)
尝试将标记放在数组中,然后在addListerner函数中传递数组项和索引,以生成对模态的顺序调用,如:
var arrayForModal = [];
for (i = 0; i < commerciallocations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(commerciallocations[i][1], commerciallocations[i][2]),
map: map,
icon:'images/commercialmapicon.png'
k = arrayForModal.push(marker);
addListenersOnPolygon(arrayForModal[k-1], 'click', function(k) { $(('#modal' + (k-1)).modal)});
});
答案 1 :(得分:0)
感谢您的帮助。这肯定有助于我的思路,我解构了我在SO上发现的一些其他功能,我设法正确地编写了点击功能。
我将每个Modal的ID添加到数组中每个位置的索引中(将id附加到标记)。然后我创建了一个变量来标识索引值。然后我简单地使用ID值调用索引,具体取决于在for循环中单击了哪个标记。
var commerciallocations = [
['Regan',43.703264,-79.804144,'#modal0'],
['Lake',43.897239,-78.6594789, '#modal1'],
['Rowe',43.72277,-80.378554, '#modal2'],
['Westport',43.649826,-79.6599653, '#modal3'],
['Vinefresh',42.9556009,-81.6699305, '#modal4'],
['Winery', 42.1209449,-82.9707676, '#modal5']
];
var markers =[];
for (var i = 0; i < commerciallocations.length; i++) {
var place = commerciallocations[i];
var marker = new google.maps.Marker({
position: new google.maps.LatLng(commerciallocations[i][1], commerciallocations[i][2]),
map: map,
icon:'images/commercialmapicon.png',
title: place[0],
popup: place[3]
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
$(this.popup).modal();
});