如何删除谷歌地图中的所有路线? [英] how to remove all routes in google map?

查看:563
本文介绍了如何删除谷歌地图中的所有路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用renderDirections和requestDirections方法来创建循环中调用的多个路由,现在我想在调用该循环之前清除它们,因为即使没有路由数据,它也会显示过去的数据。
PS:当没有数据时,方法不会被调用,但它会显示以前的路线

I am using renderDirections and requestDirections methods to create multiple routes called in a loop, now i want to clear them before calling that loop because even when there is no route data it shows that of past data. PS: The methods are not called when no data is there but it shows previous routes

以下是我设置路线的示例代码:

Below is my sample code to set routes:

 function renderDirections(result, Driver) {

        var directionsRenderer = new google.maps.DirectionsRenderer({ suppressMarkers: false,preserveViewport: true, polylineOptions: { strokeColor: colors[cur] } });
        directionsRenderer.setMap(map);
        directionsRenderer.setDirections(result);
        var leg = result.routes[0].legs[0];
        makeMarker(leg.start_location, Driver);
        makeMarker(leg.end_location, Driver);
        cur++;
    }

function requestDirections(start,end,wps,Driver){

function requestDirections(start, end, wps, Driver) {

        var directionsService = new google.maps.DirectionsService;
        directionsService.route(
            {
                origin: start,
                destination: end,
                waypoints: wps,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, function (result) {
                renderDirections(result, Driver);
            });
    }


推荐答案

directionsRender []全局设置一个计数器,循环旧路线以清除它。

This worked for me: declaring directionsRender[] globally and setting up a counter to loop on old routes to clear it `

    function renderDirections(result, Driver) {
         directionsRenderer[cnt] = new google.maps.DirectionsRenderer();
         directionsRenderer[cnt].setMap(map);
         directionsRenderer[cnt].setDirections(result);
         cnt++;
    }`
      function clearRoutes() {  
        for (var i = 0; i < cnt; i++) {
            directionsRenderer[i].setMap(null);
        }
    }

这篇关于如何删除谷歌地图中的所有路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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