Hide polyline from A to B using in Google Map api v3

前端 未结 3 806
逝去的感伤
逝去的感伤 2021-01-16 01:54

I am displaying google map with code below, I want to hide Polyline between A to B. All answers on google talk about creating an array and then doing array.setmap(null

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 02:35

    As shown in the demo below, you can remove polylines by two means:

    • setting the option suppressPolylines to true in directionsDisplay, your google.maps.DirectionsRenderer by using

      directionsDisplay.setOptions({
              suppressPolylines: true
            });
      

      This will preserve the start- and end-point markers.

      The method setOptions(options:DirectionsRendererOptions) changes the options settings of DirectionsRenderer after initialization.

    • use directionsDisplay.setMap(null); to remove all directions rendering, but this includes markers, so if you do that you will need to add extra markers to the map.

    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;
    }
    
    
    
    
    

提交回复
热议问题