How do I get an error alert generated by the Google Maps API?

13

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?

    
asked by anonymous 25.12.2013 / 13:56

2 answers

6

In the absence of a better solution (see Talles' answer), here is a workaround :

(function() {
  var original = window.alert;
  window.alert = function() {
    if (arguments[0].match(/Google has disabled use of the Maps API/))
      original("Google Maps falhou :(");
    else
      return original.apply(this, arguments);
  };
})();

This will do exactly what the title of your question suggests: Capture all calls to the alert() function and verify that it is called by Google Maps. If so, do something more friendly to the end user.

If you find a better solution (for better understand, any other), do not use this. It will fail if they change a comma in the error message, literally.

    
25.12.2013 / 15:53
3

It does not look like it:

  • If you analyze the bunch of mined JavaScript you will find alert , hardcoded .
  • The authorization documentation page also does not help much.
  • Someone in SO also came to ask this ; no response.
  • I also tried to take a look at the return statuses, but all returned 200
25.12.2013 / 14:47