0

我正在尝试使用地图中的“过境”旅行模式获得 4 个方向。但确定了每次浏览量限制为 3 次“中转”调用。我想知道我的代码是否有问题,或者此限制是否存在于 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>
4

1 回答 1

0

当您遇到该错误时,您可以“推迟”更多时间。这很明显,但似乎适用于您拥有的路线数量。

        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());
        }

例子

于 2013-05-20T13:54:46.590 回答