如何从谷歌地图中删除/移除折线

How to delete/remove polyline from Google Map?

本文关键字:折线 删除 谷歌地图      更新时间:2023-09-26

我正在尝试删除/删除该行。 但是flightPath.setMap(null)不起作用。 我正在发送JSFiddle链接,它将为您提供完整的详细信息。我的代码。
jsfiddle.net/rb9w8o5c/5/

function initialize() {
     google.maps.event.addListener(map, 'click', addLatLng);
 }
function addLatLng(event) {
        var path = poly.getPath();
        path.push(event.latLng);
        marker = new google.maps.Marker({
            position: event.latLng,
            title: '#' + path.getLength(),
            map: map
        })
        markers.push(marker);
        for (var key in markers) {
            flightPlanCoordinates.push(markers[key].position);
            positions.push(markers[key].position);
        }
        flightPath = new google.maps.Polyline({
                path: flightPlanCoordinates,
                strokeColor: "#FF0000",
                strokeOpacity: 1.0,
                strokeWeight: 2
            });    
    }
    function removePolyline(){
         flightPath.setMap(null);
    }
function removePolyline(){
        var path = poly.getPath();
        path.pop();
        marker = markers.pop();
        marker.setMap(null);
        for (var key in markers) {
            flightPlanCoordinates.push(markers[key].position);
            positions.push(markers[key].position);
        }
        flightPath = new google.maps.Polyline({
                path: flightPlanCoordinates,
                strokeColor: "#FF0000",
                strokeOpacity: 1.0,
                strokeWeight: 2
            });
}