javascript - Google Maps API - 信息窗口中的图像幻灯片
问题描述
我正在寻找一种在 Google Maps API 的信息窗口中创建图像幻灯片的方法。
这是我正在使用的代码 -
var slideIndex = 1;
$(function() {
initializeMaps();
});
function initializeMaps() {
fetch('./get_locations.php').then(response => {
return response.json();
}).then(data => {
var mapOptions = {
zoom: 6,
center: {lat: 48.208411, lng: 16.373471}
};
var spots = data;
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var markers = [];
var md5array = [];
var contentHtml = "";
for (var i=0;i<spots.length;i++) {
var infowindow = new google.maps.InfoWindow();
md5array = spots[i].md5.split(',');
if(md5array.length > 1) {
contentHtml = "";
for(var j=0;j<md5array.length;j++) {
contentHtml += '<a href=show_image.php?md5="'+ md5array[j] +'"><img class="mySlides" src="https://www.whatever.com/pics/locations/'+ md5array[j] +'.jpg" width="350px" height="350px" alt="'+ spots[i].location +'" title="'+ spots[i].location +'"></a>';
}
contentHtml = '<div>' + contentHtml + '<br><button class="slideshow-button slideshow-button-left" onclick="plusDivs(-1)">❮</button><button class="slideshow-button slideshow-button-right" onclick="plusDivs(1)">❯</button></div>';
}
else {
contentHtml = '<div><a href=show_image.php?md5="'+ spots[i].md5 +'"><img src="https://www.whatever.com/pics/locations/'+ spots[i].md5 +'.jpg" width="350px" height="350px" alt="'+ spots[i].location +'" title="'+ spots[i].location +'"></a><br></div>';
}
var spotMarker = new google.maps.Marker({
position: new google.maps.LatLng(spots[i].lat,spots[i].lng),
map: map,
zIndex: 1,
icon: '/pics/misc/pin.png',
spotContentHtml: contentHtml,
location: spots[i].location
});
google.maps.event.addListener(infowindow, 'domready', function() {
showDivs(slideIndex);
});
google.maps.event.addListener(spotMarker, 'click', function () {
console.log(this.spotContentHtml);
infowindow.setContent(this.spotContentHtml);
infowindow.open(map, this);
});
markers.push(spotMarker);
}
var mc = new MarkerClusterer(map, markers, {gridSize: 40});
}).catch(err => {
console.log(err);
});
}
function plusDivs(n) {
showDivs(slideIndex += n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex-1].style.display = "block";
}
基本上,我首先检查每个位置是否有超过 1 个图像。如果没有,我只想展示这张图片。如果有超过 1 个图像,我想创建一个幻灯片。它适用于具有多个图像的位置,但是一旦我单击只有一个图像的标记,我就会收到以下错误消息 -
Uncaught TypeError: Cannot read property 'style' of undefined
at showDivs (locations.php:213)
我认为这是由于我调用 domready 和 click 事件的顺序不正确,但不幸的是我不知道如何解决它。
提前感谢您的帮助!
解决方案
问题来自这一行(我认为因为我没有测试它):
x[slideIndex-1].style.display = "block";
下面的代码不会造成麻烦,因为您告诉脚本循环 for untili
不再小于x.length
. 因此,如果x.length
为零,则不会发生任何事情,并且您的脚本不会尝试访问不存在对象的属性。
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
但是如果你x[slideIndex-1].style.display = "block";
不检查是否x[slideIndex-1]
对应任何东西,你就会遇到问题。
解决方案:
您可以先检查以下内容的长度x
:
if (x.length) {
x[slideIndex-1].style.display = "block";
}
并确保x[slideIndex-1]
存在,即。例如,它最终不会尝试将样式应用于x[-1]
项目。
推荐阅读
- javascript - 为什么我收到 {"readyState":0,"status":0,"statusText":"NetworkError: A network error occurred."}
- python - Discord.py AFK 命令重写
- redis - 在 Scylla DB 中启用 Redis API
- docker - centos Docker中无法使用Systemctl命令
- gradle - .gradle 中的哪些路径可以在没有 Internet 的情况下重新生成?
- python - Discord bot on_member_join 不起作用 有人可以解释一下吗
- c# - 处理该方法后每隔一小时得到一次热
- java - “生成”文件夹下的文件,不应编辑“Android Studio中出现错误
- javascript - React JS Map TypeError:无法读取未定义的属性“地图”
- java - 火花作业使用本地 [*] 成功运行,但在使用 docker 映像时失败 - 独立模式下的 bitnami/spark