Google Maps API v3删除所有折线 [英] Google Maps API v3 remove all polylines

查看:115
本文介绍了Google Maps API v3删除所有折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小背景。我有一个导航设置,当你点击某个导航项时,它会在地图上创建标记。如果您点击不同的导航项目,它将删除以前的标记并设置新的标记。



现在我正在使用多段线,并试图用多段线创建相同的概念,但是我遇到了困难时间。这里是我的:

  //全局变量为行数组
var points = [];

设定我的分数。

  line1 = new google.maps.LatLng(line1Start,line1Finish); 
line2 = new google.maps.LatLng(line2Start,line2Finish);
line3 = new google.maps.LatLng(line3Start,line3Finish);

points.push(line1,line2,line3);

设定我的折线。

  var polyline = new google.maps.Polyline({
path:points,
strokeColor:#FF0000,
strokeOpacity:1.0,
strokeWeight :2
});

用线条初始化地图。

  polyline.setMap(map); 

一切正常。线条创建并显示在我的标记之间。现在,让我们删除它们(或不...)

  function removeLines(){
if(points){
points.length = 0;
}
points = [];
}

在函数的开头调用removeLines()来清除它们,那么新的设置。这实际上清除了我在points数组中的点数,但是在地图本身上多段线仍然显示出来,并且不会像我的标记一样消失。



给出什么?!

解决方案

您必须做 polyline.setMap( null),这将从地图中删除该行。 文档


Little background. I have a navigation setup for when you click on a certain navigation item, it creates markers on the map. If you click on a different navigation item, it removes the previous markers and sets up new ones.

Well now I am working with polylines and trying to create the same concept here with the polylines, however I am having a difficult time. Here is what I have:

    // Global variable for array of lines
    var points= [];

Setup my points.

    line1 = new google.maps.LatLng(line1Start, line1Finish);
    line2 = new google.maps.LatLng(line2Start, line2Finish);
    line3 = new google.maps.LatLng(line3Start,line3Finish);

    points.push(line1, line2, line3);

Setup my polylines.

    var polyline = new google.maps.Polyline({
       path:points,
       strokeColor:"#FF0000",
       strokeOpacity:1.0,
       strokeWeight:2
    });

Initialize the map with lines.

    polyline.setMap(map);

All works well. The lines are created and show up between my markers. Now lets remove them (or not...)

    function removeLines() {
      if (points) {
           points.length = 0;
      }
      points = [];
    }

removeLines() is being called at the beginning of the function to clear them, then new ones are setup. This indeed clears my points in the points array, however on the map itself the polylines still show up and do not disappear like my markers do.

What gives?!

解决方案

You have to do polyline.setMap(null), that will remove the line from the map. Documentation.

这篇关于Google Maps API v3删除所有折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆