javascript - Google Map API 路线请求限制

标签 javascript jquery html css google-maps

我正在做我的元素,我需要在 Google map 中计算 23 条行车路线的距离。我这里有代码。 Lat 和 lng 已经在 js 中设置,但我在网络中只得到 10 条路线距离。我检查了 google maps api 的限制,但我没有看到路线方向只有 10 个限制。我附上了代码....我想知道是否有人可以帮我弄清楚.....非常感谢!

<!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>

最佳答案

免费 API 有 8 个航路点的限制(加上 2 个端点为 10 个)。付费 API 允许 23 个航点。

来自 the documentation

The maximum allowed waypoints is 8, plus the origin, and destination. Maps API for Business customers are allowed 23 waypoints, plus the origin, and destination. Waypoints are not supported for transit directions.

您可以尝试将多个请求链接在一起,例如 this ,但要注意 DirectionsService 受速率限制和配额限制,因此这可能并不总是有效。

example with your points

关于javascript - Google Map API 路线请求限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946208/

相关文章:

php - 如何防止图像另存为 + 防止图像拖放到桌面 + 防止在桌面浏览器上截图

javascript - 将元素中的多个值放入数组中

jquery - 使用 css dvision 正确设置动画

html - z-index 不显示父项后面的嵌套子项

javascript - ERR_FILE_NOT_FOUND JS 和 CSS 文件在没有网络服务器的情况下在本地提供

javascript - 如何放大CSS按钮的可点击?

javascript - NPAPI - 在插件中使用 javascript 对象/函数

javascript - 验证单个单选按钮在可用的 javascript 验证脚本第 2 部分中不起作用

javascript - 使用 jquery 或 javascript 删除部分属性值

javascript - 防止命名 anchor 默认