19

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?

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • 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 Dec 13 '10 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 Dec 13 '10 at 13:56

3 Answers3

5

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) }
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
  • This works wonderfully for catch all types of API key exception. If you want to handle every type of error differently then I would do http://stackoverflow.com/a/43758757/5059472 – Karan Bhandari May 03 '17 at 11:29
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).

Community
  • 1
  • 1
Dennis G
  • 21,405
  • 19
  • 96
  • 133
  • Thanks some good information. It's not so much that I want to know if a given API key is valid, it's more that I need to handle the error if it has been set incorrectly, and appropriately not try to call a bunch of stuff on the map object (which will not exist) as well as call other google methods. – NibblyPig Dec 13 '10 at 15:42
  • Well using GMaps Version 3 you don't need keys anymore (!) http://code.google.com/apis/maps/documentation/javascript/basics.html. Honestly I think you shouldn't check for whether the GMaps Javascript has loaded, you should rather check whether the intial GMap object is available and go from there (e.g. http://stackoverflow.com/questions/832692/javascript-how-to-check-if-google-maps-is-fully-loaded) – Dennis G Dec 13 '10 at 22:18
  • 1
    Would love to use V3 but the majority of our users use IE6 and it's not compatible :( – NibblyPig Dec 15 '10 at 16:07
2

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);
Martin Höller
  • 2,714
  • 26
  • 44
Life777
  • 21
  • 2