从谷歌地图中删除折线

6
我正在使用DirectionsRenderer来显示路径,但在完成后,我想删除折线并继续进行其他操作。我似乎无法控制此函数创建的标记和折线。
有人知道如何删除这样的折线吗?如果不可能,有其他建议吗?
我知道您可以使用suppress属性,但由于我在第一阶段使用了折线,这并不能解决任何问题。
非常沮丧... 干杯!
3个回答

2
这是您问题的解决方案:要么使用suppressPolylines选项,要么从地图中删除渲染器。

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var time;

var pointA = new google.maps.LatLng(48.86, 2.35);
var pointB = new google.maps.LatLng(33.7167, 73.0667);

function initialize() {

  var rendererOptions = {
      map: map,
      draggable: true
    }
    // Instantiate a directions service.
  directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);

  // Create a map and center it on islamabad.
  var islamabad = new google.maps.LatLng(33.7167, 73.0667);
  var mapOptions = {
    zoom: 13,
    center: islamabad
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = pointA;
  var end = pointB;
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
};

function removeRoute() {
  directionsDisplay.setOptions({
    suppressPolylines: true
  });
  // this "refreshes" the renderer
  directionsDisplay.setMap(map);
};

function removeRouteNull() {
  directionsDisplay.setMap(null);
};
google.maps.event.addDomListener(window, 'load', initialize);
#map {
  height: 280px;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<button onclick="removeRoute()">Remove route (suppressPolylines)</button>
<button onclick="removeRouteNull()">Remove route (setMap(null))</button>
<button onclick="initialize()">Undo all</button>
<section id="map"></section>


0

这是如何从Google Maps v3中删除折线的方法。希望能对您有所帮助。

var line = [];
 var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates, //create an array of LatLng objects
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
 line.push(flightPath);

现在你正在将所有的折线对象推入一个名为line的数组中。你可以通过像这样循环它来使其不可见或从地图中删除:
for (i=0; i<line.length; i++) 
{                           
  line[i].setMap(null); //or line[i].setVisible(false);
}

0

使用

[self.polyline setMap:nil];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接