Google Maps API V3 - 更改旅行模式时,“方向”面板会变得混乱

时间:2012-12-24 17:13:22

标签: google-maps google-maps-api-3

我正在构建一个Google地图应用,我需要在其中提供多个旅行选项的路线,并且我在侧面板中显示路线。

但是,当我选择新的路线请求并将其结果传递给面板时,当事情搞砸时,请参阅小提琴http://jsfiddle.net/PLrkp/4/
这里是如何重现它首先选择一个方向使用公交作为旅行模式,然后改为自行车,你会在可以选择替代路线的部分看到奇怪的方框

注意:只有在提供路由替代品时才会发生这种情况。在Directions请求对象中设置为true,并且仅当实际存在多个路由时, (看起来该部分没有完全更新)

这是我的代码

    <div class="mapsection" >
      <form onsubmit="calcRoute();return false;">
          <select id="travelmode">
            <option value="DRIVING" selected="selected" data-shortMode="c">DRIVING</option>
            <option value="TRANSIT" data-shortMode="r" selected>TRANSIT</option>
            <option value="WALKING" data-shortMode="w">WALKING</option>
            <option value="BICYCLING" data-shortMode="b">BICYCLING</option>
          </select>
          <input type="text" id="start" value="Washington"/>
          <input type="text" id="end" value="New York"/>
          <button type="submit">GET DIRECTIONS</button>
      </form>
      <div id="map_canvas" style="width:100%; height:550px;float:left;"></div>
      <div id="directionsPanel" style="float:right;max-width:395px; overflow:scroll;height: 505px;overflow-x: hidden;"></div>
</div>
<script>
    //define one global Object
      var myMap = {}
      //init 
      function initialize(){
      //set up map options
        var mapOptions = {
          center: new google.maps.LatLng(40.75377,-73.990531),
          zoom: 4,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
        };
        myMap.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        myMap.directionsService = new google.maps.DirectionsService();
        myMap.directionsDisplay = new google.maps.DirectionsRenderer();
        myMap.directionsDisplay.setMap(myMap.map);
        myMap.directionsDisplay.setPanel(document.getElementById("directionsPanel"));
      }//end init
      //directions
      var calcRoute = function() {
        var start = document.getElementById("start").value,
        end = document.getElementById("end").value,
        request = {
            origin:start,
            destination:end,
            durationInTraffic :true,
            transitOptions: {
            departureTime:  new Date()
          },
          provideRouteAlternatives : true,
          travelMode: document.getElementById("travelmode").value
        };
        myMap.directionsService.route(request, function(result, status) {
          if(status == google.maps.DirectionsStatus.OK) {
            myMap.directionsDisplay.setDirections(result);
          }else{
           alert("something went wrong!");
          }
        });

      }
      //script loader
      var loadScript = function() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&sensor=false&callback=initialize";
        document.body.appendChild(script);
      }
      window.onload = loadScript;
</script>

1 个答案:

答案 0 :(得分:0)

这个问题已经修复,你可以在问题的小提琴上看到它