1

So basically what I am trying to do is have a map cluster and once you click into the clusters and click on the individule map markers you will be linked off to a different page on my website.

this is what i have so far. but i cant seem to pull the URLS through.. into different markers.

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

The event listener at the bottom doesnt work.. i cant seem to figure this out. any help would be appreciated

1
  • You're trying to assign listener to array. Use a foreach to assign individually to each marker object in that array.
    – dev8080
    Commented Apr 2, 2017 at 8:48

1 Answer 1

1

Assign listener to each marker object not to the array. Use your map function itself to avoid 2 for loops:

 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;
});
1
  • Anytime. If it helped you, pls accept it as the ans.
    – dev8080
    Commented Apr 3, 2017 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.