13
If, for some reason, an error occurs and Google disables access to the maps API, an alert of this kind will appear to the visitor:
Google has disabled use of the Maps API for this application. The provided key is not a 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: https://developers.google.com/maps/documentation/javascript/tutorial#api_key
There are several reasons that may lead Google to cut access to a certain API, but for the visitor, this information shows flaws in development and, for the programmer, not much use, because he will only know the problem when visiting the page and, effectively, view the alert, or if a more interesting visitor report 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 is loaded:
$(document).ready(function(){
//cache elements
$geoMap = $('#jMapCanvas');
});
When I need to present the map:
geoMapLat = $geoMap.data('lat');
geoMapLng = $geoMap.data('lng');
geoMapBox = $geoMap.data('infobox');
loadGmapsApi();
Question
How can I "catch" an alert that the Google Maps API sends out in order to take certain action, rather than allowing the message to be presented to the visitor?
Reproduction of the problem: jsfiddle.
– Guilherme Bernal