2012-05-04 152 views
0

我正在使用谷歌地圖api V3,有些事情出錯了路線不顯示。 這裏是代碼路線折線不顯示DirectionsRenderer的谷歌地圖v3?

function addRoute(source,destination,color){ 
     deleteOverlays(); 
     var arr = []; 
     var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); 
     var queryString = "?source=" + source+"&destination="+destination; 
     request.open("GET","url to get the stops"+ queryString, true); 
     request.send(null); 
     request.onreadystatechange = function() { 
      if (request.readyState == 4) { 
       if (request.status == 200) { 
        route = eval(request.responseText); 
        for(var i = 0; i < route.length; i++){ 
         //alert("route::"+route[i].stopname); 
         var marker = addMarker(new google.maps.LatLng(route[i].lattitude,route[i].langitude),route[i].stopname); 
         markersArray.push(marker); 
         arr.push({ location: new google.maps.LatLng(route[i].lattitude,route[i].langitude), stopover: true }); 
         map.setCenter(new google.maps.LatLng(route[i].lattitude,route[i].langitude)); 

        } 
        drawPolyline(source,destination,arr,color); 
        showOverlays(); 
       } 
      } 
     }; 
} 

Ajax響應的格式如下

[{"stopname":"Bookstore,Hamilton,NY","lattitude":42.8040014451186,"langitude":-75.548035569867},{"stopname":"Townhouse,Hamilton,NY","lattitude":42.8094640653946,"langitude":-75.5441731888855},{"stopname":"Cutten Hall,Hamilton,NY","lattitude":42.8162955973712,"langitude":-75.5402678925598},{"stopname":"Whitnall Field,Hamilton,NY","lattitude":42.8177908781254,"langitude":-75.5412549454773},{"stopname":"Person Hall,Hamilton,NY","lattitude":42.8179325344271,"langitude":-75.5369204957092},{"stopname":"Frank Dining Hall,Hamilton,NY","lattitude":42.8159650567458,"langitude":-75.5374569375122},{"stopname":"Person Hall,Hamilton,NY","lattitude":42.8178066177305,"langitude":-75.536877580365},{"stopname":"Whitnall Field,Hamilton,NY","lattitude":42.817633481854,"langitude":-75.5412334878052},{"stopname":"University Ct./Burch,Hamilton,NY","lattitude":42.8183102829785,"langitude":-75.5432934243286},{"stopname":"Parker Apartments,Hamilton,NY","lattitude":42.8182945435016,"langitude":-75.5456108529175},{"stopname":"Newell Apartments ,Hamilton,NY","lattitude":42.8179167948541,"langitude":-75.5466837365235}] 




    function drawPolyline(source,destination,waypoints,color){ 
     var polylineOptionsActual = { strokeColor: color }; 
     var directionsDisplay = new google.maps.DirectionsRenderer({ 
       suppressMarkers: true, 
       suppressInfoWindows: true, 
       polylineOptions: polylineOptionsActual 
      }); 
     var directionsService = new google.maps.DirectionsService(); 
     directionsDisplay.setMap(map); 
     //var mode = google.maps.DirectionsTravelMode.DRIVING; 
     var request = { 
        origin: source, 
        destination: destination, 
        travelMode: google.maps.TravelMode.DRIVING, 
        waypoints:waypoints 
       }; 
       directionsService.route(request, function(response, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
        directionsDisplay.setDirections(response); 
        directionsArray.push(directionsDisplay); 
        } 
       }); 
    } 

every value is going fine. 

但路徑沒有顯示,是在谷歌地圖API V3改變任何東西嗎?

+0

你可以發佈提供給drawPolyline參數?這個功能就像我一樣。 –

+0

嗨我更新了代碼在這裏,PLZ看看那... –

回答

0

假設你沒有自己的營業執照:

有最大的極限。 8個航點,但您目前有11

檢查statusdirectionsService.route()回調,你應該得到一個「MAX_WAYPOINTS_EXCEEDED」 -warning

+0

謝謝,這是問題..如何得到它的工作更多的航點... –

+0

購買營業執照(有限制23)。或者自己創建路線(我想當你知道航點時,你應該有足夠的數據來創建多段線和標記) –

+0

你能解釋如何創建路線嗎? –