2013-09-22 58 views
1

我正在研究我的項目,在該項目中我需要計算Google地圖中23條駕車路線的距離。我在這裏有代碼。 Lat和lng已經在js中建立了,但我只能在網絡中返回10個路由距離。我檢查谷歌地圖api的限制,但我沒有看到路線方向只有10個限制。我附上代碼....我想知道是否有人可以幫我弄清楚.....非常感謝!谷歌地圖API路線請求限制

<!DOCTYPE html> 
<html> 
<html> 
    <head><meta name="viewport" content="initial-scale=1.0, user-scalable=no"/><meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
     <title>Google Maps JavaScript API v3 Example: Directions Waypoints</title> 
     <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
     </script> 
     <link type="text/css" rel="stylesheet" href="https://s3.amazonaws.com/GTFS/style.css" media="all" /> 

    </head> 

<body id="mainBody" onload="initialize()"> 
<div id="headerImage"> 
<img src="https://s3.amazonaws.com/GTFS/brt.jpg" height="100" width="180" /> 
</div> 

    <header id="mainHeader"> 
     <hgroup id="mainHeaderGroup"> 
      <h1>GTFS Data Visualization & Application</h1> 
      <br /> 
      <br /> 
      <br /> 
     </hgroup> 
    </header> 
    <div id="contentContainer" > 
     <div id="map_canvas" style="width:0%;height:0%;"> 
     </div> 
     <br /> 
     <p id="demo"> 
</p> 
     <script type="text/javascript"> 
    var directionDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 

    function initialize() { 
     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var chicago = new google.maps.LatLng(-40.321, 175.54); 
     var myOptions = { 
      zoom: 6, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      center: chicago 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     directionsDisplay.setMap(map); 
     calcRoute(); 
    } 

    function calcRoute() { 
     var locations = [ 
    ['location1', 30.27361,-97.69835, 4], 
    ['location2', 30.273206,-97.700665, 5], 
    ['location3',30.272939,-97.70225, 3], 
    ['location4', 30.272556,-97.704568, 2], 
    ['location5', 30.272509,-97.706226, 4], 
    ['location6', 30.272329,-97.708247, 5], 
    ['location7', 30.271236,-97.710649, 3], 
    ['location8', 30.270419,-97.715, 2], 
    ['location8', 30.270096,-97.717001, 4], 
    ['location9', 30.269698,-97.719604, 5], 
    ['location10', 30.269427,-97.72125, 3], 
    ['location11', 30.268978,-97.724138, 2], 
    ['location12', 30.270096,-97.717001, 4], 
    ['location13', 30.269698,-97.719604, 5], 
    ['location14', 30.269427,-97.72125, 3], 
    ['location15', 30.268978,-97.724138, 2], 
    ['location16', 30.268756,-97.725507, 1], 
    ['location17',30.268704,-97.727634, 2], 
    ['location18', 30.26956,-97.729989, 4], 
    ['location19', 30.269995,-97.731193, 5], 
    ['location20', 30.270893,-97.734212, 3], 
    ['location21', 30.27116,-97.738825, 2], 
    ['location22', 30.271811,-97.741229, 2], 
    ['location23', 30.268756,-97.725507, 1] 
]; 

var i=0; 
for (i = 0; i < locations.length - 1; i++) { 
var start = new google.maps.LatLng(locations[i][1], locations[i][2]); 
var end = new google.maps.LatLng(locations[i + 1][1], locations[i + 1][2]); 
var request = { 
origin : start, 
destination : end, 
travelMode : google.maps.DirectionsTravelMode.DRIVING 
}; 
directionsService.route(request,function(response, status) { 
if (status == google.maps.DirectionsStatus.OK) { 
directionsDisplay.setDirections(response); 
var route = response.routes[0]; 
document.getElementById("demo").innerHTML += "<strong> <br /> <hr />"+  route.legs[0].distance.text + "</strong>"; 
} 
}); 
} 
} 
</script> 
</div> 
</body> 
</html> 

回答

1

免費API有8個航點的限制(加2個端點給10)。付費API允許23個航點。

the documentation

允許的最大路標數爲8,包括起點和終點。 Google Maps API for Business 客戶被允許使用23個航點,加上出發地和目的地。支持運輸路線的路點不是 。

您可以嘗試鏈接多個請求在一起,就像this,但要注意爲DirectionsService受速率限制和配額,所以這可能並不總是可行的。

example with your points

+0

非常感謝,這真的很有幫助。 – user2804616