Reputation: 13
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?
Upvotes: 1
Views: 1749
Reputation: 85
Well there's something i found in Web > Maps JavaScript API > Events under title "Listening for authentication errors"
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.
so you just need to define a global function:
function gm_authFailure() { /* Code */ };
Upvotes: 4
Reputation: 117314
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):
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);
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
}
Upvotes: 0