【问题标题】:How to fix ‘Over Query Limit’ error using Google Maps API如何使用 Google Maps API 修复“Over Query Limit”错误
【发布时间】:2019-04-09 19:17:04
【问题描述】:

这里的问题是每次我运行 Google 都会返回“Over Query Limit”。每当我使用搜索栏时,我似乎也无法从谷歌检索任何目的地建议。后一个问题似乎与前一个问题有关。 此代码用于获取用户的当前位置,允许用户插入他们的目的地,在源和目的地之间绘制一条路线,然后固定从 Google 的“方向”API 检索到的方向。 请注意,这个项目过去工作得很好,然后在某一时刻停止工作,我们开始收到错误“Over Query Limit”

我已尝试实施以下链接中建议的更改,但无济于事:

  1. I've tried this

代码如下:

       <script>
            var latlng;
             var address;
            var places; 
            var dest;
            var L;
          function initMap() {
            var markerArray = [];
            // Instantiate a directions service.
            var directionsService = new google.maps.DirectionsService;
              //geocoder
            var geocoder = new google.maps.Geocoder();
            // Create a map and center it on Manhattan.
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 20,
              center: {lat: 40.771, lng: -73.974}
            });
              //Current location
               infoWindow = new google.maps.InfoWindow;

              if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function(position) {
                 /* pos = {
                  lat: position.coords.latitude,
                  lng: position.coords.longitude
                }; */
                latlng = {lat: parseFloat(position.coords.latitude), lng: parseFloat(position.coords.longitude)};
        // This is making the Geocode request
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status !== google.maps.GeocoderStatus.OK) {
                alert(status);
            }
            // This is checking to see if the Geoeode Status is OK before proceeding
            if (status == google.maps.GeocoderStatus.OK) {
                address = (results[1].formatted_address);
            }
        });
                  var marker = new google.maps.Marker({
                    position: latlng,
                    map: map
                  });
                infoWindow.setPosition(latlng);
                infoWindow.setContent('current Location');
                infoWindow.open(map,marker);
                map.setCenter(latlng);
              }, function() {
                handleLocationError(true, infoWindow, map.getCenter());
              });
            } else {
              // Browser doesn't support Geolocation
              handleLocationError(false, infoWindow, map.getCenter());
            }
              //searchbar
               var input = document.getElementById('search');
            var searchBox = new google.maps.places.SearchBox(input);
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

            // Bias the SearchBox results towards current map's viewport.
            map.addListener('bounds_changed', function() {
              searchBox.setBounds(map.getBounds());
            });
      var markers = [];
             searchBox.addListener('places_changed', function() {
              places = searchBox.getPlaces();
              if (places.length == 0) {
                return;
              }
              // For each place, get the icon, name and location.
              var bounds = new google.maps.LatLngBounds();
              places.forEach(function(place) {
                if (!place.geometry) {
                  console.log("Returned place contains no geometry");
                  return;
                }
                var icon = {
                  url: place.icon,
                  size: new google.maps.Size(71, 71),
                  origin: new google.maps.Point(0, 0),
                  anchor: new google.maps.Point(17, 34),
                  scaledSize: new google.maps.Size(25, 25)
                };
                // Create a marker for each place.
                markers.push(new google.maps.Marker({
                  map: map,
                  icon: icon,
                  title: place.name,
                  position: place.geometry.location
                }));
                   dest = (places[0].formatted_address);
                if (place.geometry.viewport) {
                  // Only geocodes have viewport.
                  bounds.union(place.geometry.viewport);
                } else {
                  bounds.extend(place.geometry.location);
                }
              });
              map.fitBounds(bounds);
            });
            // Create a renderer for directions and bind it to the map.
            var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
            // Instantiate an info window to hold step text.
            var stepDisplay = new google.maps.InfoWindow;

            // Display the route between the initial start and end selections.
            calculateAndDisplayRoute(
                directionsDisplay, directionsService, markerArray, stepDisplay, map);
            // Listen to change events from the start and end lists.
            var onChangeHandler = function() {
              calculateAndDisplayRoute(
                  directionsDisplay, directionsService, markerArray, stepDisplay, map);
            };

            document.getElementById('search').addEventListener('change', onChangeHandler);
          }

          function calculateAndDisplayRoute(directionsDisplay, directionsService,
              markerArray, stepDisplay, map) {
            // First, remove any existing markers from the map.
            for (var i = 0; i < markerArray.length; i++) {
              markerArray[i].setMap(null);
            }
            // Retrieve the start and end locations and create a DirectionsRequest using
            // Driving directions.
            directionsService.route({
              origin: address,
              destination: document.getElementById('search').value,
              travelMode: 'DRIVING',
              provideRouteAlternatives: true
            }, function(response, status) {
              // Route the directions and pass the response to a function to create
              // markers for each step.
              if (status === 'OK') {
                  var routesSteps = [];
                var routes = response.routes;
                var colors = ['red','blue','purple'];
    console.log(response.routes);
                for (var i = 0; i < routes.length; i++) {
                            // Display the routes summary
                document.getElementById('warnings-panel').innerHTML += 'Route ' + i + ': via ' + routes[i].summary + '<br />';
                    new google.maps.DirectionsRenderer({
                        map: map,
                        directions: response,
                        routeIndex: i,
                        polylineOptions: {
                            strokeColor: colors[i],
                            strokeWeight: 4,
                            strokeOpacity: .2
                        }
                         });
                }
                showSteps(response, markerArray, stepDisplay, map);
              } else {
                window.alert('Directions request failed due to ' + status);
              }
            });
          }
          function showSteps(directionResult, markerArray, stepDisplay, map) {
              var x = directionResult.routes.length;
              var arr = [];
               console.log("Length",directionResult.routes.length);
               for (var i = 0; i <= x-1; i++) {
                   //console.log( directionResult.routes[i].legs[0].steps.length);
                   arr[i] = directionResult.routes[i].legs[0].steps.length;
                   console.log( directionResult.routes[i].legs[0]);  
               }
              console.log("array",arr);
              //console.log("Min", Math.min.apply(null, arr));
              console.log("Min", arr.indexOf(Math.min.apply(null, arr)));
              L = arr.indexOf(Math.min.apply(null, arr));

               //var myRoute = directionResult.routes[x-1].legs[0];
               var myRoute = directionResult.routes[L].legs[0];
            for (var i = 0; i < myRoute.steps.length; i++) {
              var marker = markerArray[i] = markerArray[i] || new google.maps.Marker;
              marker.setMap(map);
              marker.setPosition(myRoute.steps[i].start_location);
              attachInstructionText(
                  stepDisplay, marker, myRoute.steps[i].instructions, map);
            }  
          }
          function attachInstructionText(stepDisplay, marker, text, map) {
            google.maps.event.addListener(marker, 'click', function() {
              stepDisplay.setContent(text);
              stepDisplay.open(map, marker);
            });
          }
        </script>

【问题讨论】:

  • 你是如何加载 API 的?你包括钥匙吗?您的密钥的结算帐户是否有关联的信用卡?
  • @geocodezip 是的,我们正在使用密钥,但不,我没有与之关联的信用卡。
  • 那(没有与密钥的计费帐户关联的信用卡)可以解释过度查询限制问题。
  • @geocodezip 我同意,但正如我在问题中提到的那样,它之前确实有效,当时我没有列出任何信用卡。
  • 我怀疑政策最近发生了变化(或者您的“免费”配额用完了)。

标签: javascript google-maps google-maps-api-3 reverse-geocoding google-geocoding-api


【解决方案1】:

您的积分可能已用完。

我建议通过https://console.cloud.google.com/google/maps-apis/support 提交支持案例。因为它可能涉及您分享有关您的项目和计费帐户的个人身份信息。强文本

【讨论】:

  • 我已经多次检查代码并得出结论,我的信用额度几乎用尽或耗尽​​我的免费配额。此外,我上网并使用了我的 API 密钥,发现每次运行代码时我只使用了 19 个请求,这与每天 2500 个请求的免费配额相去甚远。
  • 最好提交支持案例以查明真相。由于您遇到配额错误,这很可能与您的结算帐户发生了一些变化有关。
  • 只是让您知道新版 Google 地图平台中没有“免费配额”。您所指的“免费配额”是旧模型“Google Maps API”。 Google Maps Platform 遵循这一新定价cloud.google.com/maps-platform/pricing/sheet
猜你喜欢
  • 2012-12-10
  • 2018-04-18
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多