从google map api v3中删除路线

dav*_*ern 36 javascript jquery google-maps google-maps-api-3

我有一个使用API​​ v3的谷歌地图,它可以获取从一个位置到另一个位置的路线.该应用程序运行良好,但获取方向的窗口是地图上的叠加.我喜欢它,所以当这个窗口关闭时,方向将从地图上移除,但其他标记仍然存在.

我尝试过以下方法:

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

这似乎隐藏了预期的窗口,但没有做任何关于从地图中删除方向的问题.

任何帮助深表感谢.

戴夫.

Jos*_*hin 52

您可以将DirectionsRenderer的地图绑定更改为"null"以删除方向叠加

directionDisplay.setMap(null);
Run Code Online (Sandbox Code Playgroud)

  • 类似地,如果您使用面板来显示方向,您可以通过执行`directionDisplay.setPanel(null)来清除它; (4认同)

小智 48

你可以试试这个,而不是丢失对地图的引用

directionDisplay.set('directions', null);
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案:它允许在不重新初始化地图的情况下显示/隐藏方向。 (2认同)

bha*_*rat 8

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