Change marker icon options in google maps

百般思念 提交于 2019-12-23 05:26:33

问题


I use this code to create my markers (TypeScript btw) The options for the icon allows me to rotate and set a fill color of the marker. But how can I change the fill color and rotation after creation?

I have found various responses here on SO, but they refer to changing the icon to a bitmap and swap between red and green bitmaps for example. Which is not what I want.

        var icon = {
            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
            scale: 4,
            fillColor: "#ff5050", //<-- I want to change this after creation
            fillOpacity: 1,
            strokeWeight: 1,
            rotation: 0 //<-- I want to change this after creation
        };

        var markerOptions = <google.maps.MarkerOptions>{               
            map: this.map,                
            icon: icon,
        };

Anyone know?


回答1:


Calling .setIcon on the marker works for me:

setInterval(function () {
    angle += 30;
    cnt++;
    icon.rotation = angle;
    icon.fillColor = colorArray[cnt % colorArray.length]
    marker.setIcon(icon);
}, 1000);

working fiddle

working code snippet:

var map;
var angle = 0;
var marker;
var icon = {
            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
            scale: 4,
            fillColor: "#ff5050", //<-- I want to change this after creation
            fillOpacity: 1,
            strokeWeight: 1,
            anchor: new google.maps.Point(0, 5),
            rotation: 0 //<-- I want to change this after creation
        };
var colorArray = ["#ff0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF", "#FF00FF"];
var cnt = 0;
function init() {
    var startLatLng = new google.maps.LatLng(50.124462, -5.539994);

    map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: startLatLng,
        zoom: 12
    });

    var ptMarker = new google.maps.Marker({
        position: new google.maps.LatLng(50.124462, -5.539994),
        map: map,
        icon: {
            url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
            size: new google.maps.Size(7, 7),
            anchor: new google.maps.Point(4, 4)
        }
    });
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(50.124462, -5.539994),
        icon: icon
    });
    marker.setMap(map);
    var circleMarker = new google.maps.Marker({
        position: new google.maps.LatLng(50.124462, -5.539994),
        map: map,
        icon: {
            path: google.maps.SymbolPath.CIRCLE,
            scale: 24,
            strokeWeight: 2,
            fillColor: '#009933',
            fillOpacity: 0.001,
            anchor: new google.maps.Point(0, 0)
        }
    });
    
    setInterval(function () {
        angle += 30;
        cnt++;
        icon.rotation = angle;
        icon.fillColor = colorArray[cnt % colorArray.length]
        marker.setIcon(icon);
    }, 1000);

}

google.maps.event.addDomListener(window, 'load', init);
html, body, #map-canvas {
    height: 100%;
    width: 100%;
    margin: 0px;
    padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>


来源:https://stackoverflow.com/questions/30149817/change-marker-icon-options-in-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!