javascript中验证谷歌地图api密钥

javascript中验证谷歌地图api密钥

本文介绍了如何在html javascript中验证谷歌地图api密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个相当标准的 Google 地图页面,使用我自己的 API 密钥.该页面仅通过 JS 添加一些标记并将其呈现在文本框中.如果密钥无效或过期等,则显示以下页面.

那很好,但是我想至少告诉用户为什么它是错误的.地图通过标准访问:

...<脚本类型=文本/javascript"src="http://maps.google.com/maps/api/js?key=API_KEY"></script><身体><div id="地图"></div><p><input type="text" "readonly" id="status" size=40></p>...

但是,我想检查 API 密钥是否有效且有效,并在该文本框中告诉用户密钥是否正常.

我该怎么做?

附注.在 src= 中使用 callback 函数的问题在于,无论 API 状态如何,它都会返回,但实际上并不是状态本身.这里是一个kludge解决方案.

编辑:我在模糊和记录不佳 回调函数,gm_authFailure(),并通过以下方式解决了我的问题:

function gm_authFailure() {let apiError = "没有或无效的 API 密钥!"document.getElementById('status').value = apiError;};

您甚至不需要在 URL 中指定此回调,它无论如何都会使用它!

解决方案

添加:

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

在全局范围内.据此:

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

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.com/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!

解决方案

Add this:

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

In the global scope. Per this:

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

这篇关于如何在html javascript中验证谷歌地图api密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:06