2017-04-02 175 views
0

所以基本上我想要做的是有一個地圖集羣,一旦你點擊進入集羣並點擊個人地圖標記,你將鏈接到不同的頁面在我的網站上。在標記集羣中添加地圖標記的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

回答

1

分配監聽器到每個標記對象不是陣列。使用您的地圖功能本身,以避免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