-1

I have a google map that I would like to display two types of marker on. One of these would display a popup window with some information, the other would simply link to a URL.

Is it possible to set some sort of distinction between markers so that you can add separate listener functions to each?

Thanks in advance.

MrFirthy
  • 67
  • 6
  • possible duplicate of [Select Link or Checkbox to Display Category Markers](http://stackoverflow.com/questions/10900395/select-link-or-checkbox-to-display-category-markers) – Anto Jurković Jun 23 '14 at 09:34
  • See also [Google Map API V3: How to add Custom data to markers](http://stackoverflow.com/questions/11378450/google-map-api-v3-how-to-add-custom-data-to-markers) – Anto Jurković Jun 23 '14 at 09:39

1 Answers1

0

Something like this maybe (http://jsfiddle.net/uzV95/)? :

var pos = [
        {
            position: new google.maps.LatLng(-25.1, 131.044922),
            action: function (e) {
                document.location.href = "http://google.com"
            }
        },
        {
            position: new google.maps.LatLng(-25.363882, 131.044922),
            action: function (e) {
                infowindow.open(map, this);
            }
        }
    ],
    markers = [],
    infowindow = new google.maps.InfoWindow({
        content: "hi"
    }),
    mapOptions = {
        center: pos[0].position,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    },
    map = new google.maps.Map(document.getElementById("map"),
                              mapOptions);

pos.forEach(function (position) {
    var marker = new google.maps.Marker({
        position: position.position
    });
    marker.setMap(map);
    google.maps.event.addListener(marker, 'click', position.action);
    markers.push(marker);
});

Hope this helps!

Thomas Eschemann
  • 995
  • 7
  • 18