确定Google Maps API在运行时是否使用了无效密钥 [英] Determine if Google Maps API is using an invalid key during runtime

查看:113
本文介绍了确定Google Maps API在运行时是否使用了无效密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来确定是否正在使用有效的Google地图API密钥。使用无效的密钥,我们收到一个错误,指出谷歌API已被禁用。我想要捕获该回报,并根据该回报确定是否执行我们的地理编码功能。

现在,当我们保存记录时,我们会查看地址是否已更改。如果有,我们得到该地址的地理编码,并在成功/失败消息传回后,我们继续进行处理,然后再保存记录。

禁用API时,代码会停止。什么都不会返回 - 没有成功或失败。此时,我们的代码停止以及我们依赖返回,所以我们知道该从哪里去。我们并没有寻找解决授权问题的方法,只是一种确定API是否在运行时被禁用的方法。任何想法?

解决方案

不幸的是,没有实现选项来检测禁用的API。

去年,我制作了 功能请求 ,但我不认为会发生任何事情。



两种可能的选项(可能会有更多):


  1. 当您使用地图时,通常API会首先尝试创建地图(一些元素将添加到地图 - 容器 )。当API被禁用时,这些元素将被删除。所以你可能会做这样的事情发生时(例如,设置一个变量,指示禁用状态):

    pre $ document $ document.getElementById(' map')// map-container
    .addEventListener('DOMNodeRemoved',function(e){
    if(!this.children.length){
    alert('API has已被禁用');
    if(window.google&& window.google.maps){
    window.google.maps.disabled = true;
    }
    }
    },false);


  2. 覆盖警报 - 方法截获特定消息:

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
    if(s.indexOf('Google has disabled use of the Maps API for this application。')
    === 0){

    if(window.google&& amp ; window.google.maps){
    window.google.maps.disabled = true;
    alert('API已禁用');
    return;
    }

    f(

    ))(window.alert)




  3. 在以上两种情况下,您都可以使用<$ c $(自定义)禁用属性c> google.maps 来检查API是否被禁用:

      if(google.maps .disabled === true){
    //执行替代代码
    }


    I'm looking for a way to determine whether a valid Google maps API key is being used. Using an invalid key, we receive an error stating that the google API has been disabled. I'd like to capture that return, and determine whether or not to do our geocoding functions based on that return.

    As it stands, when we save a record, we look to see if the address has been changed. If it has, we get the geocode of that address, and after the success/failure message has been passed back, we continue on with our processing before we save the record.

    When the API is disabled, the code simply stops. Nothing is returned - no success or failure. At this point, our code stops as well as we rely on the return so we know where to go from there. We are not looking for a way to get around licensing, simply a way to determine whether the API is disabled at runtime. Any ideas?

    解决方案

    Unfortunately there is no implemented option to detect a disabled API.

    Last year I've made a feature-request , but I don't think that something will happen.

    two possible options(there may be more):

    1. when you use a map usually the API first will try to create the map(some elements will be added to the map-container ). When the API will be disabled, these elements will be removed. So you may do something when this happens(e.g. set a variable that indicates the disabled-status):

       document.getElementById('map-canvas')//map-container
        .addEventListener ('DOMNodeRemoved', function(e){
            if(!this.children.length){
              alert('the API has been disabled');
              if(window.google && window.google.maps){
                window.google.maps.disabled=true;
              }
            }
          }, false);
      

    2. override the alert-method to intercept the particular message:

      (function(f){
        window.alert=function(s){
          if(s.indexOf('Google has disabled use of the Maps API for this application.')
             ===0){
      
            if(window.google && window.google.maps){
              window.google.maps.disabled=true;
              alert('the API has been disabled');
              return;
            }
          }
          f(s);
        }
      })(window.alert)
      

    In both cases you may later use the (custom)disabled-property of google.maps to check if the API has been disabled:

    if(google.maps.disabled===true){
      //execute alternative code
    }
    

    这篇关于确定Google Maps API在运行时是否使用了无效密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆