How to verify a google maps api key in html javascript? - Stack Overflow

I have a rather standard Google Maps page, using my own API key. The page only does some marker additio

I have a rather standard Google Maps page, using my own API key. The page only does some marker additions by JS and presenting it in a text-box. If the key is invalid or expired or whatever, the following page is displayed.

That is fine, but then I would like to at lest tell the user why it is wrong. The map is accessed by standard:

...
<script type="text/javascript" 
        src=""></script>
</head>
<body>
<div id="map"></div> 
<p><input type="text" "readonly" id="status" size=40></p>
...

However, I would like to check that the API key is working and valid, and tell the user if the key is OK or not in that text box.

How can I do that?

PS. The problem with using a callback function in the src=, is that it returns regardless what is the API status, but not actually the status itself. Here is a kludge solution of this.


EDIT: I found an answer in the obscurely and poorly documented callback function, gm_authFailure(), and solved my question with:

function gm_authFailure() { 
    let apiError = "No or invalid API key!"
    document.getElementById('status').value = apiError; 
};

You don't even need to specify this callback in your URL, it will use it anyway!

I have a rather standard Google Maps page, using my own API key. The page only does some marker additions by JS and presenting it in a text-box. If the key is invalid or expired or whatever, the following page is displayed.

That is fine, but then I would like to at lest tell the user why it is wrong. The map is accessed by standard:

...
<script type="text/javascript" 
        src="http://maps.google./maps/api/js?key=API_KEY"></script>
</head>
<body>
<div id="map"></div> 
<p><input type="text" "readonly" id="status" size=40></p>
...

However, I would like to check that the API key is working and valid, and tell the user if the key is OK or not in that text box.

How can I do that?

PS. The problem with using a callback function in the src=, is that it returns regardless what is the API status, but not actually the status itself. Here is a kludge solution of this.


EDIT: I found an answer in the obscurely and poorly documented callback function, gm_authFailure(), and solved my question with:

function gm_authFailure() { 
    let apiError = "No or invalid API key!"
    document.getElementById('status').value = apiError; 
};

You don't even need to specify this callback in your URL, it will use it anyway!

Share edited Mar 4, 2018 at 22:15 not2qubit asked Mar 4, 2018 at 21:43 not2qubitnot2qubit 17.2k9 gold badges118 silver badges160 bronze badges 1
  • Have you found a way to get the specific error message details from within gm_authFailure function ? – Fausto R. Commented Oct 1, 2019 at 13:45
Add a ment  | 

1 Answer 1

Reset to default 4

Add this:

function gm_authFailure() { alert('yo'); };

In the global scope. Per this:

https://developers.google./maps/documentation/javascript/events#auth-errors

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933602a4601883.html

相关推荐

  • How to verify a google maps api key in html javascript? - Stack Overflow

    I have a rather standard Google Maps page, using my own API key. The page only does some marker additio

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信