gpt4 book ai didi

javascript - 在标记集群谷歌地图 API 中添加 URL 到 map 标记

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:26 25 4
gpt4 key购买 nike

所以基本上我想做的是拥有一个 map 集群,一旦您单击集群并单击单个 map 标记,您将被链接到我网站上的另一个页面。

这就是我到目前为止所拥有的。但我似乎无法将 URL 拉入不同的标记。

   var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -39.3642, lng: 164.0444318}
});


// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
url: "http://travelpark.co.nz" //Will be different for each marker
});

});


// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}


var locations = [
{lat: -37.7862985, lng: 175.2773836},
{lat: -37.8011434, lng: 174.871265}
]

google.maps.event.addListener(markers, 'click', function() {
window.location.href = this.url;
});

底部的事件监听器不起作用..我似乎无法弄清楚这一点。任何帮助将不胜感激

最佳答案

将监听器分配给每个标记对象而不是数组。使用您的 map 函数本身来避免 2 个 for 循环:

 var markers = locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
url: "http://travelpark.co.nz" //Will be different for each marker
});
google.maps.event.addListener(marker, 'click', function(){
window.location.href = this.url;
});
return marker;
});

关于javascript - 在标记集群谷歌地图 API 中添加 URL 到 map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166323/

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