javascript - 单击时不会弹出信息窗口
问题描述
我正在尝试获取已设置为具有动态内容的标记的信息窗口,但我遇到的问题是单击时根本不会打开信息窗口。
我相信我可能以错误的方式实现代码信息窗口。
代码
var mosqueMarker, count;
for (count = 0; count < mosqueLocations.length; count++){
mosqueMarker = new google.maps.Marker({
position: new google.maps.LatLng(mosqueLocations[count][1], mosqueLocations[count][2]),
title: mosqueLocations[count][0],
map: map
})
}
var mosqueInfowindow = new google.maps.InfoWindow({})
mosqueMarker.addListener('click', function(count) {
mosqueInfowindow.setContent(mosqueLocations[count][0]),
console.log(mosqueLocations)
mosqueInfowindow.open(map, mosqueMarker)
})
解决方案
你需要改变(至少)三件事:
- 在循环内移动
mosqueMarker
事件监听器 count
获取循环中变量的函数闭包- 用于
this
引用侦听器函数内的标记(否则它将始终在循环中的最后一个标记上打开信息窗口)。
for (var count = 0; count < mosqueLocations.length; count++) {
var mosqueMarker = new google.maps.Marker({
position: new google.maps.LatLng(mosqueLocations[count][1], mosqueLocations[count][2]),
title: mosqueLocations[count][0],
map: map
});
mosqueMarker.addListener('click', (function(count) {
return function(evt) {
mosqueInfowindow.setContent(mosqueLocations[count][0]),
console.log(mosqueLocations)
mosqueInfowindow.open(map, this)
}})(count))
}
代码片段:
// The following example creates complex markers to indicate beaches near
// Sydney, NSW, Australia. Note that the anchor is set to (0,32) to correspond
// to the base of the flagpole.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {
lat: -33.9,
lng: 151.2
}
});
setMarkers(map);
}
// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.
var mosqueLocations = [
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
];
function setMarkers(map) {
var mosqueInfowindow = new google.maps.InfoWindow({})
for (var count = 0; count < mosqueLocations.length; count++) {
var mosqueMarker = new google.maps.Marker({
position: new google.maps.LatLng(mosqueLocations[count][1], mosqueLocations[count][2]),
title: mosqueLocations[count][0],
map: map
});
mosqueMarker.addListener('click', (function(count) {
return function(evt) {
mosqueInfowindow.setContent(mosqueLocations[count][0]),
console.log(mosqueLocations)
mosqueInfowindow.open(map, this)
}
})(count))
}
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
推荐阅读
- azure-active-directory - Azure AD 中基于组的许可 - 获取直接许可和继承许可 AzureAD 模块
- python - 使用python json模块的JSON解码器错误
- oracle - Oracle APEX:交互式网格 PL/SQL 流程
- java - Arrays.copyOf 不返回对象的深层副本
- java - 将 Java 日历转换为 ISO 的最简单方法
- bash - Bash:从引用字符串块中的每个引用字符串创建一个变量
- sql - SQL 子查询从 if else 语句返回超过 1 个值
- javascript - OpenStreetMap - 如何使用可下载的数据库?
- python - XQuery 到 API Python 字符串
- symfony - Symfony 4,从数据夹具类中获取根路径目录