2012-03-26 55 views
1

有人发出新的方向请求后,我很难清除Google Maps API中的数据。谷歌地图在新请求后显示旧方向

让我来解释一下情况:用户来到网站并决定获取到我们位置的路线。用户现在填写他或她所在的位置。用户得到的数据很好,但问题是当他/她进入一个新的位置时,旧的方向会留在面板中。我想清楚它可以使用这行代码。

directionsDisplay.setPanel(null); 

我有这个问题,我无法弄清楚它在代码中的位置(是的,我是新来的js)。

这是我到目前为止有:

function calculateDirections(locFrom, locTo) { 
    var directionsDisplay = new google.maps.DirectionsRenderer(); 
    var directionsService = new google.maps.DirectionsService(); 
    var mapContainer = $('#contactInfo'); 
    var mapPanel = $('#mapPanel'); 
    var request = { 
     origin: locFrom, 
     destination: 'Address 01, City', 
     travelMode: google.maps.TravelMode.DRIVING 
    }; 
    var myOptions = { 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: locFrom 
    }; 

    directionsService.route(request, function(result, status) { 
     if(status == google.maps.DirectionsStatus.OK) { 
      // Build frame 
      var map = new google.maps.Map(document.getElementById('maps'), myOptions); 
      directionsDisplay.setPanel(document.getElementById("mapPanel")); 
      directionsDisplay.setDirections(result); 
      directionsDisplay.setMap(map); 
      $('#contactInfo').css('top', '30px'); 
      $('#contactInfo').css('padding-bottom', '15px'); 
     } 
     else { 
      alert('Could not calculate route'); 
     } 
    }); 
} 

我想,我会先从directionsDisplay.setPanel(NULL);并在此之后加载数据,以便在每次迭代中开始清除数据。但是,我对这项任务感到厌倦。帮助将不胜感激。

编辑:

根据记录,应用程序有1个固定点(我们的业务的位置)和locFrom是从用户的位置(这可以在任何地方)。

编辑:

下面是标记:

<div id="maps"></div> 
    <div id="menu"> 
    <div class="contentWrapperLeft"></div> 
    <div id="contactInfoRoute"> 
     <h2>Route</h2>   
       <input type="text" name="street" id="street" rel="Straat"> 
     <input type="text" name="place" id="place" rel="Plaats"> 
     <input type="button" name="submit" id="submit" value="Route"> 
    </div> 

     <div id="contactInfo"> 
      <div id="mapPanel"></div> 
      <p>Address 01 <br /> City, Country</p> 
     </div> 
    </div> 

回答

1

我想出了解决办法如下:

我所做的是写一个空mapPanel DIV之前的请求 了这样:

mapPanel.html(''); 

我把这个线在变量'地图'上方,这使得它工作没有任何问题。

0

我觉得locFrom是好像当你按一下按钮你不发送新的价值观的问题?尝试捕捉locfrom以查看动作后是否更新?因为通常为了映射apiis而不需要进行任何重大修改。