Javascript, Change google map marker color

后端 未结 9 1228
自闭症患者
自闭症患者 2020-12-07 11:44

May I know a way to change the Google Map marker color via Javascript.. I am new at this and any help would be much appreciated, Thank you.

I used the following code

相关标签:
9条回答
  • 2020-12-07 12:20

    To expand upon the accepted answer, you can create custom MarkerImages of any color using the API at http://chart.googleapis.com

    In addition to changing color, this also changes marker shape, which may be unwanted.

    const marker = new window.google.maps.Marker(
            position: markerPosition,
            title: markerTitle,
            map: map)
    marker.addListener('click', () => marker.setIcon(changeIcon('00ff00'));)
    
    const changeIcon = (newIconColor) => {
        const newIcon = new window.google.maps.MarkerImage(
            `http://chart.googleapis.com/chart?                                      
            chst=d_map_spin&chld=1.0|0|${newIconColor}|60|_|%E2%80%A2`,
            new window.google.maps.Size(21, 34),
            new window.google.maps.Point(0, 0),
            new window.google.maps.Point(10, 34),
            new window.google.maps.Size(21,34)
        );
        return newIcon
    }
    

    Source: free udacity course on google maps apis

    0 讨论(0)
  • 2020-12-07 12:23

    You can use the strokeColor property:

    var marker = new google.maps.Marker({
        id: "some-id",
        icon: {
            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
            strokeColor: "red",
            scale: 3
        },
        map: map,
        title: "some-title",
        position: myLatlng
    });
    

    See this page for other possibilities.

    0 讨论(0)
  • 2020-12-07 12:27

    var map_marker = $(".map-marker").children("img").attr("src") var pinImage = new google.maps.MarkerImage(map_marker);

         var marker = new google.maps.Marker({
          position: uluru,
          map: map,
          icon: pinImage
        });
      }
    
    0 讨论(0)
提交回复
热议问题