Javascript 从 google map api v3 中删除路线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5645920/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:15:08  来源:igfitidea点击:

Remove directions from google map api v3

javascriptjquerygoogle-mapsgoogle-maps-api-3

提问by daveredfern

I have a google map using API v3 which gets directions from one location to another. The app works great but the window which gets the directions is an overlay on the map. I'd like it so when this window is closed directions are removed from the map but other markers remain.

我有一个使用 API v3 的谷歌地图,它可以获取从一个位置到另一个位置的路线。该应用程序运行良好,但获取方向的窗口是地图上的叠加层。我希望如此,当此窗口关闭时,方向将从地图中删除,但其他标记保留。

I have tried the following:

我尝试了以下方法:

$('#content .close').live('click', function() {
$('#content').hide();
directionDisplay = new google.maps.DirectionsRenderer();
directionDisplay.suppressMarkers = true;
directionDisplay.setMap(map);
return false;
});

This seems to hide the window as expected but doesn't do anything regards with removing directions from the map.

这似乎按预期隐藏了窗口,但对从地图中删除方向没有任何作用。

Any help is much appreciated.

任何帮助深表感谢。

Dave.

戴夫。

回答by Josnidhin

You can change the map binding for the DirectionsRenderer to "null" to remove the direction overlay

您可以将 DirectionsRenderer 的地图绑定更改为“null”以移除方向叠加

directionDisplay.setMap(null);

回答by Jimmy Collazos

You can try this, and not lose reference to the map

你可以试试这个,不要失去对地图的参考

directionDisplay.set('directions', null);

回答by bharat

You can also use : directionsDisplay.setDirections({routes: []});

您还可以使用: directionsDisplay.setDirections({routes: []});

回答by webewitch

That should read as:

那应该读作:

directionDisplay.setMap(null);

回答by Friendly Code

None of the above worked for me, this is what I needed:

以上都不适合我,这就是我需要的:

// Clear past routes
    if (directionsDisplay != null) {
        directionsDisplay.setMap(null);
        directionsDisplay = null;
    }

回答by user2314737

Using directionDisplay.setMap(null);will remove the whole directions renderer overlay, including markers. If you just want to remove the routes keeping the markers you can use setOptionsto change the options settings of DirectionsRenderer for suppressPolylinesafter initialization

使用directionDisplay.setMap(null);将删除整个方向渲染器覆盖,包括标记。如果您只想删除保留标记的路线,您可以在初始化后setOptions更改 DirectionsRenderer 的选项设置suppressPolylines

directionsDisplay.setOptions({
    suppressPolylines: true
  });

(see also my other similar answer)

(另见我的其他类似答案