Google Maps API v3:处理使用限制限制

时间:2012-09-03 10:50:47

标签: javascript jquery jquery-ui google-maps

Google地图对API使用情况有限制: 所有Web服务每个Web服务的速率限制为每秒10个请求。 Google Places API具有单独的使用限制。 参考:Google Maps API Usage Limit

我向Google Maps API发送了10多个请求。 以下是我使用JQuery.ui.maps编写的示例代码:

 $('#map').gmap('displayDirections', {
             'origin': new google.maps.LatLng(42.645573,-71.098326),
             'destination': new google.maps.LatLng(42.345573,-72.298326),
             'travelMode': google.maps.DirectionsTravelMode.DRIVING },
             {suppressMarkers: true},function(success, result) {
                if ( success )
                   alert('Results found!');
             });

我已根据我的要求更新了以下jquery函数。 我为每个请求添加了1秒的睡眠/延迟。

displayDirections: function(current, directionsRequest, directionsRendererOptions, callback) {
        alert("displayDirections "+current);
        var startTime = new Date().getTime(); // get the current time
            while (new Date().getTime() < startTime + 1000) {
            } // hog cpu
        var self = this;        
        var directionService = this.get('services > DirectionsService', new google.maps.DirectionsService());

        var directionRenderer =new google.maps.DirectionsRenderer(directionsRendererOptions);

        directionService.route(directionsRequest, function(results, status) {
            alert("directionService Called " + current);
            var startTime = new Date().getTime(); // get the current time
            while (new Date().getTime() < startTime + 1000) {
            } // hog cpu
            if ( status === 'OK' ) {
                directionRenderer.setDirections(results);
                directionRenderer.setMap(self.get('map'));
            } else {
                alert(status)
                directionRenderer.setMap(null);
            }
            self.get('directionRenderers').push(directionRenderer);

            callback(results, status);
        });
    }

但它没有按要求运作。 有人可以通过展示如何解决这个限制来帮助我。

如果需要更多详细信息,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:2)

你可以检查'OVER_QUERY_LIMIT'的状态,如果你得到这个,那么你可以延迟几秒钟,比如$ .delay(3000):http://api.jquery.com/delay/

您是否超过每日限额或10 /秒限制?

相关问题