简体   繁体   English

公交出行方式的路线限制

[英]Limits for Directions with Transit Travel Mode

I'm trying to get 4 directions with "Transit" Travel Mode in a Map. 我正在尝试在地图上通过“公交”旅行模式获得4个方向。 But a limitation of 3 "transit" calls per pageview was identified. 但是,每个页面浏览量限制为3个“转接”调用。 I was wondering if there was something wrong with my code, or if this limitation exists in the paid version of the Google Maps API V3. 我想知道我的代码是否有问题,或者Google Maps API V3的付费版本中是否存在此限制。

<script>
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    var coords = [];
    var coordsIndex = 0;

    function initialize() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var chicago = new google.maps.LatLng(41.850033, -87.6500523);
        var mapOptions = {
            zoom: 7,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: chicago
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        directionsDisplay.setMap(map);

        coords.push("-23.544721,-46.784817");
        coords.push("-23.571372,-46.644323");
        coords.push("-23.509219,-46.602631");
        coords.push("-23.512052,-46.343422");
        coords.push("-23.719354,-46.41552");
        coordsIndex = 0;
        calcRoute();

    }

    function calcRoute() {

        var start = coords[coordsIndex];
        var end = coords[coordsIndex + 1];

        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.TRANSIT
        };
        directionsService.route(request, function (response, status) {
            coordsIndex++;
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);

                var description = '';
                for (ndxRoute = 0; ndxRoute < response.routes.length; ndxRoute++) {

                    var route = response.routes[ndxRoute];

                    for (ndxLeg = 0; ndxLeg < route.legs.length; ndxLeg++) {

                        var leg = route.legs[ndxLeg];

                        if (description != '') description += '<br />';
                        description += leg.start_location + ' -> ' + leg.end_location + '<br />';

                        for (ndxStep = 0; ndxStep < leg.steps.length; ndxStep++) {
                            var step = leg.steps[ndxStep];
                            description += '    ' + step.instructions + '<br />';
                        }
                    }

                }

                document.all['instructions'].innerHTML += description;
            }
            else
                window.alert('Error calling route method: ' + status.toString());
        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#map-canvas, #map_canvas {
  height: 80%;
}

@media print {
  html, body {
    height: auto;
  }

  #map_canvas {
    height: 650px;
  }
}

#panel {
  position: absolute;
  top: 5px;
  left: 50%;
  margin-left: -180px;
  z-index: 5;
  background-color: #fff;
  padding: 5px;
  border: 1px solid #999;
}
</style>
<body>
    <div id="map-canvas">
    </div>
    <button id="nextButton" onclick="javascript:calcRoute()">
        Call next route</button>
    <div id="instructions">
    </div>
</body>

You can "hold off" for increasing amounts of time when you get that error. 出现该错误时,您可以“推迟”更多的时间。 It is noticeable but seems to work for the number of routes you have. 这很明显,但是似乎可以解决您拥有的路线数量。

        else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
            coordsIndex--;
            errorCount++
            if (errorCount < 3)
              setTimeout("calcRoute()", 2000*errorCount);
        else
              window.alert('Error calling route method ['+coordsIndex+']: ' + status.toString());
        }

example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM