Search code examples
google-mapsgoogle-maps-api-3polylinegoogle-polyline

Google Maps add remove Polyline


i've a question about 'removing Polylines' from Google Maps. In this document is explained, how can be added or removed a polyline. (https://developers.google.com/maps/documentation/javascript/examples/polyline-remove?hl=de)

But my situation is a little bit different. I've a 'addPolyline' function, which adds polyline on Google Maps. With this function i can add Polylines, so i can return 'polyline Object', but i can't remove this polyine with same way.

If you click on 'addLines', it can be drawn a line, but if i click on removeLine, it will not be removed.

var map;
var flightPlanCoordinates;

function addPolyline(el){
    polyName = new google.maps.Polyline({
        path: el,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
  });
   return polyName;
}


function initialize() {
  var mapOptions = {
    zoom: 3,
    center: new google.maps.LatLng(0, -180),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

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

  flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];  
}

function addLine(el) {
  el.setMap(map);
}

function removeLine(el) {
  el.setMap(null);
}

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

Here is Fiddle : http://jsfiddle.net/aldimeola1122/gna00zwb/

How can i achieve that, can you help me? Thanks in advance.


Solution

  • You are creating a new polyline, then removing it.

    <input onclick="removeLine(addPolyline(flightPlanCoordinates));" type=button value="Remove line">
    

    If you want to remove the existing polyline, you need to keep a reference to it.

    <input onclick="removeLine(polyline);" type=button value="Remove line">
    <input onclick="polyline=addLine(addPolyline(flightPlanCoordinates));" type=button value="Add line">
    

    working fiddle

    var map;
    var flightPlanCoordinates;
    var polyline;
    
    function addPolyline(el) {
      polyName = new google.maps.Polyline({
        path: el,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
      });
      return polyName;
    }
    
    
    function initialize() {
      var mapOptions = {
        zoom: 3,
        center: new google.maps.LatLng(0, -180),
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
    
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
      flightPlanCoordinates = [
        new google.maps.LatLng(37.772323, -122.214897),
        new google.maps.LatLng(21.291982, -157.821856),
        new google.maps.LatLng(-18.142599, 178.431),
        new google.maps.LatLng(-27.46758, 153.027892)
      ];
    }
    
    function addLine(el) {
      el.setMap(map);
      return el;
    }
    
    function removeLine(el) {
      el.setMap(null);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map-canvas {
      height: 100%;
      margin: 0px;
      padding: 0px
    }
    #panel {
      position: absolute;
      top: 5px;
      left: 50%;
      margin-left: -180px;
      z-index: 5;
      background-color: #fff;
      padding: 5px;
      border: 1px solid #999;
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="panel">
      <input onclick="removeLine(polyline);" type=button value="Remove line">
      <input onclick="polyline=addLine(addPolyline(flightPlanCoordinates));" type=button value="Add line">
    </div>
    <div id="map-canvas"></div>