google map API v3 change marker icon on click?

前端 未结 3 1042
误落风尘
误落风尘 2021-02-07 08:02

Following google documentation up to changing the marker icon was easy. But I\'m stuck, I don\'t know how to change image when marker is clicked?

My code so far:

相关标签:
3条回答
  • 2021-02-07 08:17

    I have modified the previous fiddle and enabled to deselect the marker.

    http://jsfiddle.net/zsw76pq9/

    google.maps.event.addListener( map,'click', function () {
        infoWindow.close();
    
        //Change the marker icon
        marker.setIcon('https://www.google.com/mapfiles/marker_black.png');
    });
    
    0 讨论(0)
  • 2021-02-07 08:34

    You need to set the marker icon in the click event.

    google.maps.event.addListener(marker, 'click', function() {
              infowindow.open(map);
              //Change the marker icon
              marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
         });
    

    Here is an example: jsfiddle

    0 讨论(0)
  • 2021-02-07 08:39

    Try this.

    //open onclick
       google.maps.event.addListener(marker, 'click', function() {
        marker.setIcon("Your Image");                                    
        infowindow.open(map);
        });
    
    0 讨论(0)
提交回复
热议问题