【问题标题】:Google Maps API V3 - Directions panel gets messed up when changing travel modeGoogle Maps API V3 - 更改旅行模式时,路线面板变得混乱
【发布时间】:2012-12-24 17:13:22
【问题描述】:

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

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

注意:这仅在 Directions 请求对象中的 'provideRouteAlternatives' 设置为 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>

【问题讨论】:

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


    【解决方案1】:

    这个问题已经解决了,你可以在问题中的小提琴上看到它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多