2015-12-12 35 views
0
下面

是代碼,我在我的程序寫入對谷歌地圖繪製折線如何在谷歌地圖除去折線

假設response.SelectedTruck.length = 150

值包含在response.SelectedTruck被緯度和經度

我不得不推米可變

  //code for draw polyline 
      for (var i = 0; i < response.SelectedTruck.length; i++) { 
         var m = response.SelectedTruck[i]; 
          (function (n) { 
           setTimeout(function() { 
           path.push(new google.maps.LatLng(parseFloat(n.lat), parseFloat(n.lang))); 
            addPolyline(path); 
           }, i * 1000); 
           }(m)); 
           } 
          function addPolyline(m) { 
           var userCoordinate = new google.maps.Polyline({ 
            path: m, 
            strokeColor: 'blue', 
            strokeOpacity: 2, 
            strokeWeight: 2, 
            icons: [{ 
             icon: lineSymbol, 
              offset: '100%' 
              }] 
             }); 
            userCoordinate.setMap(map); 
            } 

所有的緯度和經度值這個代碼我上繪製折線地圖和我選擇第一個ID,然後它會

是繪製折線在地圖上,但之後我選擇第二個ID,然後在老

折線不是在map.so刪除如何刪除折線,如果我選擇其他ID?

+0

我有一個類似的問題,但仍無法得到它的工作,繼承人我的堆棧溢出的問題鏈接:http://stackoverflow.com/questions/36449124/for -angular-google-maps-how-do-i-remove-the-polyline – AngularM

回答

1

由於您只在一張地圖上引用。您必須在先前繪製的多段線上有參考,並將其映射爲空。

做到這一點,最簡單的方法是在全球範圍內宣佈你userCoordinate變量,並創建一個新的折線

var userCoordinate; 



       ............. 
       ............. 

     function addPolyline(m) { 
       userCoordinate.setMap(null); 
       userCoordinate = new google.maps.Polyline({ 
           path: m, 
           strokeColor: 'blue', 
           strokeOpacity: 2, 
           strokeWeight: 2, 
           icons: [{ 
            icon: lineSymbol, 
             offset: '100%' 
             }] 
            }); 

       userCoordinate.setMap(map); 
      } 

另一種選擇是創建一個新的地圖,並用它來新折線之前設置地圖爲空(但它會導致你的地圖重新加載)

  function addPolyline(m) { 
       map = new google.maps.Map(document.getElementById('map'), { 
        zoom: 3, 
        center: {lat: 0, lng: -180}, 
        mapTypeId: google.maps.MapTypeId.TERRAIN 
       }); 

       var userCoordinate = new google.maps.Polyline({ 
           path: m, 
           strokeColor: 'blue', 
           strokeOpacity: 2, 
           strokeWeight: 2, 
           icons: [{ 
            icon: lineSymbol, 
             offset: '100%' 
             }] 
            }); 

           userCoordinate.setMap(map); 
      } 
+0

我有一個類似的問題,但仍然無法得到它的工作,繼承人鏈接到我的堆棧溢出問題:http://stackoverflow.com/questions/36449124/for-angular-google-maps-how-do-i-remove-the-polyline – AngularM