在标记集群中添加地图标记的URL谷歌地图API

问题描述:

所以基本上我想要做的是有一个地图集群,一旦你点击进入集群并点击个人地图标记,你将链接到不同的页面在我的网站上。在标记集群中添加地图标记的URL谷歌地图API

这是我到目前为止。但我似乎不能通过..拉到不同的标记URLS。

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; 
    }); 

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

+0

您试图分配侦听器数组。使用foreach分别指定该数组中的每个标记对象。 – dev8080

分配监听器到每个标记对象不是阵列。使用您的地图功能本身,以避免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; 
}); 
+0

非常感谢,这工作得很好。 – Brett

+0

随时。如果它对你有帮助,请接受它作为答案。 – dev8080