Javascript, Change google map marker color

后端 未结 9 1229
自闭症患者
自闭症患者 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

提交回复
热议问题