如何通过点击更改折线颜色?

时间:2014-05-10 13:32:59

标签: javascript google-maps-api-3

我正在使用google maps api提供的绘图工具,我希望在绘制折线之后,通过点击它来改变颜色 这是我的代码

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(-34.397, 150.644),
        zoom: 8
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

    var drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: true,
        drawingControlOptions: {
            position: google.maps.ControlPosition.TOP_CENTER,
            drawingModes: [
                google.maps.drawing.OverlayType.MARKER,
                google.maps.drawing.OverlayType.POLYLINE
            ]
        },
        markerOptions: {
            icon: 'images/beachflag.png'
        },
        circleOptions: {
            fillColor: '#ffff00',
            fillOpacity: 1,
            strokeWeight: 5,
            clickable: true,
            zIndex: 1
        }
    });

    drawingManager.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:0)

您必须在initialize()函数中添加事件polylinecomplete的侦听器并定义click事件侦听器,例如:

google.maps.event.addListener(drawingManager, 'polylinecomplete', function(polyline) {
    google.maps.event.addListener(polyline, 'click', function() {
        polyline.setOptions({strokeColor: '#FF0000'});
    });
});