最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I catch an invalid API key for google maps - Stack Overflow

programmeradmin4浏览0评论

I have this code:

<script src=";amp;v=2&amp;sensor=false&amp;key=babab" type='text/javascript'></script> 

If the key is invalid then it pops up an alert, but I want to perform some action in this case. I'm not sure how to hook into it though. Any ideas?

I have this code:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=babab" type='text/javascript'></script> 

If the key is invalid then it pops up an alert, but I want to perform some action in this case. I'm not sure how to hook into it though. Any ideas?

Share Improve this question asked Dec 13, 2010 at 12:42 NibblyPigNibblyPig 52.9k75 gold badges217 silver badges378 bronze badges 2
  • 3 Hmmm, interesting. I don't think GMaps have a mechanism to check this. You could try to wrap alert() and parse the text (to see if it's the error message), but that looks like an ugly hack. – Piskvor left the building Commented Dec 13, 2010 at 12:56
  • 1 Assign the alert function to a variable, replace the alert function with something else before the script include, and replace it back when the include is done... – tshao Commented Dec 13, 2010 at 13:56
Add a comment  | 

3 Answers 3

Reset to default 5

Google does not offer an external method of checking the Google Maps API key. Hence you cannot query some service with e.g. "Is this code valid abcde1234" and get a TRUE/FALSE response.

There is a discussion on how the Maps API key is generated. But I suggest you look at a post from Mike Williams about the GValidateKey function. This is the function actually doing the magic validation - what it exactly does, like creating a hash from your Google account / domain - we don't know.

I see two solutions for your problem of checking whether the API key provided is correct:

  1. Overwrite the incoming alert with some custom code (check for the content of the alert, or check if an alert occurs withing X seconds after page load)
  2. Somehow get the GValidateKey function to validate your key beforehand. Maybe you can call it before referencing the API Javascript? Sounds kind of hackish to me...

The problem you will likely have is that you don't know what Google actually checks. The referrer, the referring site, the host - many possibilities (it is not the IP address of the server, but the name plus some additional information).

I just ran across the need to perform an action if an invalid API key was used. Google's documentation states:

If you want to programmatically detect an authentication failure (for example to automatically send an beacon) you can prepare a callback function. If the following global function is defined it will be called when the authentication fails.

This was all I needed to do:

function gm_authFailure() { // Perform action(s) }

For modern browsers (IE9+ and others) you may use DOMNodeRemoved event. You just need to add event handler to the element that you pass to the map constructor:

var map = new google.maps.Map(element, myOptions);

 element.addEventListener("DOMNodeRemoved", function(e){
   if (e.target === element){
     //your code here
     element.removeEventListener("DOMNodeRemoved", mapWasRemovedHandler, true);
   }
 }, false);
发布评论

评论列表(0)

  1. 暂无评论