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

Asked

Viewed 345 times

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?

2 answers

6

In the absence of a better solution (see Talles response), 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 function alert() and check if it is the call by Google Maps. If it is, do something more user-friendly.

If you find a better solution (however you want it, any other), don’t use it. It will fail if they change a comma in the error message, literally.

  • 2

    Remember when using this Ambia.. err.. workaround to carry it early (if possible before all scripts).

  • I consider anything else that works better than this solution. But do you have any suggestions on how to implement this? It may be interesting to read the code of the maps to see under what conditions this message is displayed. So try to check the conditions instead of the message itself (besides somehow preventing it from calling the Alert).

3

It seems there’s no way:

  • If you analyze the gang Javascript minified will find there the alert, hardcoded.
  • To page with documentation of authorisation problems doesn’t help much either.
  • Someone in the OR in English also reached the ask that; unanswered.
  • I also tried to take a look at the return status, but all returned 200 (OK): imagem do alert

Browser other questions tagged

You are not signed in. Login or sign up in order to post.