Javascript Google MAP API V3无法清除以前的多路径历史记录

Javascript Google MAP API V3无法清除以前的多路径历史记录,javascript,google-maps-api-3,Javascript,Google Maps Api 3,这是备用路由功能,而当我重定向到其他方向时。它无法清除以前的路线 function initialize() { // Create a new map with some default settings var myLatlng = new google.maps.LatLng(-37.8602828,145.079616); var myOptions = { zoom:8, center: myLatlng, mapTypeId: google.maps.MapTypeId

这是备用路由功能,而当我重定向到其他方向时。它无法清除以前的路线

function initialize() {

// Create a new map with some default settings
var myLatlng = new google.maps.LatLng(-37.8602828,145.079616);
var myOptions = {
  zoom:8,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

 //click function call calculateAndDisplayRoute to ge the alternative routes
        var directionsService = new google.maps.DirectionsService;



        document.getElementById('submit').addEventListener('click', function() {

            calculateAndDisplayRoute(directionsService, map);

        });


//this function used to calculate the alternative route.
function calculateAndDisplayRoute(directionsService, map) {     

//get the value from start and end input box 
var start = document.getElementById('start').value;
var end =document.getElementById('end').value;

//property when dran on the map
var directionsRequest = {
   //starting point
    origin: start,
    //destination
    destination: end,
    //multiple route
    provideRouteAlternatives: true,
    travelMode: google.maps.TravelMode.DRIVING

};

directionsService.route( directionsRequest, function(response, status) {

    if (status === google.maps.DirectionsStatus.OK) {
         //store the multiple routes in respones and display one by one
         for (var i = 0, len = response.routes.length; i < len; i++) {
              new google.maps.DirectionsRenderer({
                map: map,
                directions: response,
                routeIndex: i
              });
        }

    } else {
        window.alert('Directions request failed due to ' + status);
    }
});
}
函数初始化(){
//使用一些默认设置创建新地图
var mylatng=new google.maps.LatLng(-37.8602828145.079616);
变量myOptions={
缩放:8,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
//单击函数调用calculateAndDisplayRoute以显示备选路由
var directionsService=新的google.maps.directionsService;
document.getElementById('submit')。addEventListener('click',function(){
计算显示路线(方向服务、地图);
});
//此函数用于计算备选路线。
函数calculateAndDisplayRoute(方向服务,地图){
//从“开始”和“结束”输入框中获取值
var start=document.getElementById('start').value;
var end=document.getElementById('end').value;
//属性在地图上显示dran时
变量方向请求={
//起点
来源:start,
//目的地
目的地:完,
//多路
ProviderRouteAlternatives:正确,
travelMode:google.maps.travelMode.DRIVING
};
路由(DirectionRequest,函数(响应,状态){
if(status==google.maps.directionstatus.OK){
//将多个路由存储在respones中并逐个显示
对于(变量i=0,len=response.routes.length;i
如果需要更改/删除渲染方向,则需要保留对用于在地图上显示方向的
google.maps.DirectionsRenderer
对象的引用,并在丢失该引用之前将其从地图中删除



相关问题:相关问题:@geocodezip可能重复,非常感谢,希望不会太晚。@geocodezip,我记得在javascript中使用了全局变量。就像方向=[]在一个函数中为它指定一个新值,然后在另一个函数中再次读取该变量。但是该变量总是空的。你怎么做呢?谢谢
 // in the global scope
 directions = [];

document.getElementById('submit').addEventListener('click', function () {
if (directions && directions.length > 0) {
  for (var i=0; i<directions.length; i++)
     directions[i].setMap(null);
  }
  directions = [];
  calculateAndDisplayRoute(directionsService, map);
});