If for some reason an error occurs and Google disables access to the maps API, an alert of this genre will appear to the visitor:
Google has disabled use of the Maps API for this application. The provided key is not valid Google API Key, or it is not authorized for the Google Maps Javascript API v3 on this site. If you are the owner of this application, you can learn about obtaining a valid key here: link
There are a number of reasons why Google can cut access to a particular API, but for the visitor, this information shows development flaws, and for the programmer, it's not much use, because the developer will only know the problem when visit the page, and effectively view the alert, or if a visitor is more interested in reporting the situation.
code
/*! Load Google Maps API
* ------------------------------ */
function loadGmapsApi() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=aChaveParaApi&sensor=false&callback=initializeMap";
document.body.appendChild(script);
}
/*! Initialize the MAP
* ------------------------------ */
function initializeMap(){
if ($geoMap.is(':empty')) {
var myLatlng = new google.maps.LatLng(geoMapLat, geoMapLng);
var mapOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
panControl: false,
scaleControl: false,
streetViewControl: false,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
position: google.maps.ControlPosition.TOP_CENTER
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.TOP_CENTER
}
}
var map = new google.maps.Map(document.getElementById("jMapCanvas"), mapOptions);
var infowindow = new google.maps.InfoWindow({
content: $('#jMapInfoBoxContents').html()
});
var marker = new google.maps.Marker({
position : myLatlng,
map : map,
title : geoMapBox
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
infowindow.open(map,marker);
}
}
When the document loads:
$(document).ready(function(){
//cache elements
$geoMap = $('#jMapCanvas');
});
When I need to display the map:
geoMapLat = $geoMap.data('lat');
geoMapLng = $geoMap.data('lng');
geoMapBox = $geoMap.data('infobox');
loadGmapsApi();
Question
How can I "catch" an alert that the Maps API issues in order to take a certain action instead of allowing the message to be displayed to the visitor?