Javascript 用谷歌地图删除路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5232756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove route with google map
提问by Cedric Dugas
I got a small app that use the Direction Service feature of Google Map. It is working well, I can change routes, but I have a small problem, where a user could go back to a list of markers after tracing a route.
我有一个使用 Google Map 的方向服务功能的小应用程序。它运行良好,我可以更改路线,但我有一个小问题,用户可以在跟踪路线后返回到标记列表。
I can't find a way to delete routes with google map. For markers I store them and do setMap(null)
, do not see a way here...
我找不到用谷歌地图删除路线的方法。对于我存储它们并执行的标记setMap(null)
,在这里看不到方法......
回答by thomaux
You could also use:
您还可以使用:
directionsDisplay.setDirections({routes: []});
In this way, you can keep using one map with one renderer for all routes.
通过这种方式,您可以继续为所有路线使用一张地图和一个渲染器。
回答by Tomik
if you are using DirectionsRenderer to display the routes, you can call setMap(null) on it too. That way you delete the displayed route.
如果您使用 DirectionsRenderer 来显示路线,您也可以在其上调用 setMap(null)。这样您就可以删除显示的路线。
Using the example code here http://code.google.com/apis/maps/documentation/javascript/examples/directions-simple.html
使用这里的示例代码 http://code.google.com/apis/maps/documentation/javascript/examples/directions-simple.html
just call
打电话
directionsDisplay.setMap(null);
回答by shubhamkes
Since on each call, new instance of DirectionRenderer created thats why each new instance is unaware of previous instance.
因为在每次调用时,都会创建新的 DirectionRenderer 实例,这就是为什么每个新实例都不知道以前的实例。
Move
移动
var directionsDisplay = new google.maps.DirectionsRenderer();
to the global(at the top where all other Global variables have been initialized.)
到全局(在所有其他全局变量已初始化的顶部。)
By doing so, each time you would be using single instance of DirectionRenderer.
通过这样做,每次您都将使用 DirectionRenderer 的单个实例。
回答by Friendly Code
This is the fix
这是修复
// Clear past routes
if (directionsDisplay != null) {
directionsDisplay.setMap(null);
directionsDisplay = null;
}
回答by joshuahedlund
The other answers did not work for me. I found a solution from this question
其他答案对我不起作用。我从这个问题中找到了解决方案
define directionsDisplay
only 1 time(outside of the click
-handler)
directionsDisplay
仅定义1 次(在click
-handler之外)
回答by safian syed
I had similar problem, I tried with directionsDisplay.setMap(null);
but it didn't work.
我有类似的问题,我尝试过,directionsDisplay.setMap(null);
但没有用。
The problem was the directionsDisplay
object which I created was declared locally.
问题是directionsDisplay
我创建的对象是在本地声明的。
I changed it to global and every time the function is called, it will use the same global directionsDisplay
object and make changes to it. This will definitely remove the previous route displayed on same object.
我将其更改为 global 并且每次调用该函数时,它将使用相同的全局directionsDisplay
对象并对其进行更改。这肯定会删除显示在同一对象上的先前路线。
回答by Rajkumar
set strokeWeight: 0 then polyline will hide
设置 strokeWeight: 0 然后折线将隐藏
回答by Szél Lajos
It's important - at least it was in my case - that while directionsDisplay
can be declared globally the instance has to be created after the map object otherwise it gave a reference error
重要的是 - 至少在我的情况下 - 虽然directionsDisplay
可以全局声明,但必须在映射对象之后创建实例,否则它会给出引用错误
var map;
var directionsDisplay;
function initMap() {
map = new google.maps.Map(...);
directionsDisplay = new google.maps.DirectionsRenderer(...);
}
function yourFunction() {
// your code
directionsDisplay.addListener(...);
}