0

기본적으로 내가하려는 것은지도 클러스터가있는 것이고 일단 클러스터를 클릭하고 개별 맵 마커를 클릭하면 다른 페이지로 연결됩니다 내 웹 사이트에.마커 클러스터에서지도 마커에 URL을 추가 google지도 API

이것은 내가 지금까지 가지고있는 것입니다. 하지만 난 .. 통해 다른 마커로 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; 
    }); 

하단의 이벤트 수신기는 작동하지 않습니다.이 문제를 파악하는 것 같습니다. 어떤 도움을받을 수 있습니까?

+0

배열에 리스너를 지정하려고합니다. foreach를 사용하여 해당 배열의 각 표식 객체에 개별적으로 할당합니다. – dev8080

답변

1

배열이 아닌 각 표식 개체에 수신기를 지정하십시오. 2 for 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

언제든지. 그것이 당신을 도운다면, pls는 그것을 ans로 받아들입니다. – dev8080

관련 문제