Google Maps API: open url by clicking on marker

后端 未结 6 2184
小蘑菇
小蘑菇 2020-12-04 10:55

I want to open a new window by clicking on a marker using Google Maps API 3.

Unfortunately there are not many examples for the Google Maps API and I found out this c

6条回答
  •  不思量自难忘°
    2020-12-04 11:32

    url isn't an object on the Marker class. But there's nothing stopping you adding that as a property to that class. I'm guessing whatever example you were looking at did that too. Do you want a different URL for each marker? What happens when you do:

    for (var i = 0; i < locations.length; i++) 
    {
        var flag = new google.maps.MarkerImage('markers/' + (i + 1) + '.png',
          new google.maps.Size(17, 19),
          new google.maps.Point(0,0),
          new google.maps.Point(0, 19));
        var place = locations[i];
        var myLatLng = new google.maps.LatLng(place[1], place[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: flag,
            shape: shape,
            title: place[0],
            zIndex: place[3],
            url: "/your/url/"
        });
    
        google.maps.event.addListener(marker, 'click', function() {
            window.location.href = this.url;
        });
    }
    

提交回复
热议问题