2012-02-16 52 views
0

我使用Google地圖JS API創建了我自己的樣式地圖。我想補充的方向功能,地圖,我跟着official tutorial(下顯示DirectionsResult冠軍),但最終的路線沒有出現......Google地圖API:路線結果不顯示

我調試我的代碼,我發現,比directionsService.route函數返回google.maps.DirectionsStatus.OKdirectionsDisplay.setDirections(result)真的沒有JS錯誤調用...所以方向計算成功,但沒有顯示在我的地圖。 A試圖關閉特殊的地圖樣式,但即使在默認地圖樣式中也沒有出現。任何想法都可能是問題所在?

OK,一些代碼...:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAB2gbXmG... ...mNs66iPr8&amp;sensor=false&amp;callback=directions_init"></script> 

    <script type="text/javascript"> 
     var map; 
     var zoom = 11; 
     var directionsDisplay; 
     var directionsService; 

     function gmap_init(){ 
      var styleArray = [ /*here is style but even if its commented its not working*/]; 

      var alterMapStyle = new google.maps.StyledMapType(styleArray, {name: "ALTER style"}); 


      var latlng = new google.maps.LatLng(/*lat, lng*/); 
      var myOptions = { 
       zoom: zoom, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP, 
       mapTypeControl: false, 
       panControl: false, 
       zoomControl: false, 
       scaleControl: false, 
       streetViewControl: false 
      }; 
      map = new google.maps.Map(document.getElementById("gmap"), myOptions); 

      map.mapTypes.set('ALTER_style', alterMapStyle); 
      map.setMapTypeId('ALTER_style'); 


     } 

     function directions_init(){ 

      directionsService = new google.maps.DirectionsService(); 
      directionsDisplay = new google.maps.DirectionsRenderer(); 
      directionsDisplay.setMap(map); 

      display_route(); 

     } 

     function display_route(){ 
      var request = { 
       origin: 'Place A', 
       destination:'Place B', 
       travelMode: google.maps.TravelMode.DRIVING 
       }; 

      directionsService.route(request, function(result, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       //program got here without error 
       directionsDisplay.setDirections(result); 
      } 
      }); 

     } 
+0

請發表一些代碼 – slawekwin 2012-02-16 10:12:08

回答

1

不知道,如果它是你的問題的來源,但它肯定值得一試... 從腳本輸入

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAB2gbXmG... ...mNs66iPr8&amp;sensor=false&amp;callback=directions_init"></script> 

我假設在下載api腳本之後調用directions_init函數,該函數可能在頁面的onload事件之前,因此您的映射對象尚未啓動且爲空。所以,實際上你調用

directionsDisplay.setMap(null); 

從gmap_init或與該onload事件後觸發任何情況下嘗試調用directions_init。

+0

其工作,謝謝! – 2012-02-16 13:04:46

相關問題