简体   繁体   English

超过谷歌地图 API 请求限制处理程序

[英]Exceeding Google Maps API request limit handler

I'm working on creating handlers for Google Maps's possible errors.我正在为 Google 地图可能出现的错误创建处理程序。

One scenario I wanted to handle was the one where the number of requests made reaches the quota limit for the Google Maps JavaScript API.我想要处理的一种情况是,发出的请求数量达到了 Google 地图 JavaScript API 的配额限制。

When the request limit is reached, an error message covers the map.达到请求限制时,一条错误消息将涵盖 map。 The error message reads: This page can't load Google Maps correctly.错误消息显示:此页面无法正确加载谷歌地图。

There is also a message output to the console, You have exceeded your request quota for this API.控制台还有一条消息 output,您已超出此 API 的请求配额。 See https://developers.google.com/maps/documentation/javascript/error-messages?utm_source=maps_js&utm_medium=degraded&utm_campaign=billing#api-key-and-billing-errors请参阅https://developers.google.com/maps/documentation/javascript/error-messages?utm_source=maps_js&utm_medium=degraded&utm_campaign=billing#api-key-and-billing-errors

This is all working as expected.这一切都按预期工作。 Ideally, I would like to redirect the user to another page when the request limit is reached;理想情况下,当达到请求限制时,我想将用户重定向到另一个页面; however, I am totally stumped as to where I would handle this.但是,我完全不知道在哪里处理这个问题。 Below is a code sample I use to load the Google Maps JavaScript API.下面是我用来加载 Google 地图 JavaScript API 的代码示例。

import { useState, useEffect } from 'react'
import { Loader } from "@googlemaps/js-api-loader"

function useGoogleMapsApi() {
    const [api, setApi] = useState()
  
    useEffect(() => {
        if ((window as any).google) {
            setApi((window as any).google)
            return
        }
        
        const loader = new Loader({
            apiKey: 'MY_GOOGLE_MAPS_API_KEY',
            version: 'beta',
            libraries: ['places', 'geometry'],
        })
    
        loader
            .load()
            .then(() => {
                setApi((window as any).google)
            })
    }, [])
    
    return api
}

export default useGoogleMapsApi

The rate limit is the amount of API requests that can be performed within a certain time interval.速率限制是在一定时间间隔内可以执行的 API 请求的数量。 According to Google's API Usage and Billing page , there is a limit of 50 requests per second.根据Google 的 API Usage and Billing page ,每秒有 50 个请求的限制。 If you exceed this amount, subsequent requests to the API will fail with the OVER_DAILY_LIMIT or OVER_QUERY_LIMIT status code and the geocoded data will not be returned.如果超过此数量,对 API 的后续请求将失败并显示OVER_DAILY_LIMITOVER_QUERY_LIMIT状态代码,并且不会返回地理编码数据。

Google might also block your access to the API if you repeatedly go over the limit.如果您反复 go 超出限制,Google 也可能会阻止您访问API。

This is why I would advise you to avoid trying to exceed this request limit, seeing as though it could cause quite the trouble.这就是为什么我会建议您避免尝试超过此请求限制,因为它可能会造成相当大的麻烦。

Source: What happens when you exceed the Google Geocoding API rate limit?资料来源: 当您超过 Google 地理编码 API 速率限制时会发生什么?

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

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