gpt4 book ai didi

javascript - Google map API InfoWindows

转载 作者:行者123 更新时间:2023-11-28 06:13:23 25 4
gpt4 key购买 nike

我有一个带有多个标记的 Google map 。我试图在我的信息窗口中显示每个标记的信息:

function initializeMaps() {
var latlng = new google.maps.LatLng(59.4920, 37.3010);
var myOptions = {
zoom: 6,
center: latlng,
disableDefaultUI: false,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
var infoWindow = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(document.getElementById('map'),myOptions);

var mc = new MarkerClusterer(map);
var marker, i;
for (i = 0; i < data.markers.length; i++) {
var image = "/images/markers/green-marker.png";
marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng),
map: map,
title: data.markers[i].name,
icon: image,
});
/*jshint loopfunc: true */
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {

for (i=0; i<data.markers.length; i++) {
infoWindow.setContent("<div style = 'min-width:200px;min-height:40px'>" + '<img src="' + data.markers[i].link + '" style="height:150px;width:250px;" />' + "<br />" + data.markers[i].description + "<br />" + "<button>Read More</button>" + "</div>");
infoWindow.open(map, marker);
}

});
})(marker, data);
latlngbounds.extend(marker.position);

mc.addMarker(marker);
}
var bounds = new google.maps.LatLngBounds();
}

但它只向我显示 map 上每个标记信息窗口上数组的最后一个元素信息。下面是我的 JSON 文件:

var data = {
"markers":[
{
"city": "City Sample Text",
"name": "Name Sample Text",
"shortname": "redsquare_location",
"description": "Descr Sample Text",
"lat": "51.4910",
"lng": "31.2985",
"link": "images/infowindows/rs_rp.png",
},
{
"city": "City Sample Text",
"name": "Name Sample Text",
"shortname": "pchurch_location",
"description": "Descr Sample Text",
"lat": "51.4925",
"lng": "31.3007",
"link": "images/infowindows/pc_rp.png",
},
{
"city": "City Sample Text",
"name": "Name Sample Text",
"shortname": "muschool_location",
"description": "Decr Sample Text",
"lat": "51.4932",
"lng": "31.3054",
"link": "images/infowindows/ms_rp.png",
}
]
}

我的错误是什么?

最佳答案

因此你只能看到最后一个元素信息

var marker, i;
for (i = 0; i < data.markers.length; i++) {
var image = "/images/markers/green-marker.png";
marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng),
map: map,
title: data.markers[i].name,
icon: image,
});

google.maps.event.addListener(marker, "click", function (e) {
...
});
}

您正在尝试在“for”循环内设置事件监听器。所以,你的情况与 Javascript multiple dynamic addEventListener created in for loop - passing parameters not working 非常相似。

如果您使用如下代码,此代码将起作用:

data.markers.forEach(function(marker){
var image = "/images/markers/green-marker.png";
var mapMarker = new google.maps.Marker({
position: new google.maps.LatLng(marker.lat, marker.lng),
map: map,
title: marker.name,
icon: image,
});
google.maps.event.addListener(marker, "click", function(e) {
...
});
mc.addMaker(mapMarker);
})

关于javascript - Google map API InfoWindows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36181682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com