2

I have a problem with Google Maps Javascript Api. I want to detect if my daily limit quota of 25,000 map loads per 24 hours as described here is reached.

I already searched for a lot of similar questions, as [1, 2, 3, 4], but they:

  • have no useful response (1)
  • use other Api and not Google Maps Javascript Api (2)
  • refers to geocoding and not map loading (3)
  • use Google Api Console (4)

The only question that I find useful is this. It refers to Google documentation, that says:

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. function gm_authFailure() { /* Code */ };

If I have a simple script like this:

    <script>
    function initMap() {
        var map;
        map = new google.maps.Map(document.getElementById('map'), {
            zoom: 7,
            center: { lat: 42.345, lng: 12.46 }
        });
    }
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"></script>

How can I implement function gm_authFailure(), in order to detect query limit reached?

I'm new to Js and have no clue how to do that. Can you give me some examples?

Alex
  • 599
  • 1
  • 4
  • 16

1 Answers1

2

you just need to define your function globally, like this

 <script>
    function initMap() {
        var map;
        map = new google.maps.Map(document.getElementById('map'), {
            zoom: 7,
            center: { lat: 42.345, lng: 12.46 }
        });
    }
    function gm_authFailure() { 
        alert("test"); // here you define your authentication failed message
    };
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"></script>
<div id="map" style="height: 500px; width: 500px;"></div>
  • Thank you for your reply. Do you know any methods to test this code? How can I programmatically call the script in order to finish my calls? I tried to call initMap recursivelly, but it didn't work – Alex Aug 11 '17 at 14:18
  • Actually this method runs when application connect with google map and authentication is failed, but you can make some logic like => in gm_authFailure function if limit exceed then hide your map showing message about limit exceed – Muhammad Akber Khan Aug 11 '17 at 14:21
  • Also can you show your code, as you are saying that it is in loop, can you show me how actually you are using – Muhammad Akber Khan Aug 11 '17 at 14:23
  • Sorry for bad formatting, but I just called something like: `for (i = 0; i < 50000; i++) { var map; map = new google.maps.Map(document.getElementById('map'), { zoom: 7, center: { lat: 42.345, lng: 12.46 } }); }` – Alex Aug 11 '17 at 14:33
  • seeing your code, i understand , that you are showing 5000 map on single page, is this your requirement? if i am not wrong, are you want to add multiple markers/pin on map? – Muhammad Akber Khan Aug 11 '17 at 14:35
  • My requirement is to finish my key query limit. I tried to load the map multiple times not because I need 50000 map, but because I need to finish google query limit. If there is another method to do it, I will try. – Alex Aug 11 '17 at 15:20
  • You might also want to star the following feature request in Google issue tracker: https://issuetracker.google.com/issues/35830575. It requests a programmatic way to check current usage for Google Maps APIs. – xomena Aug 11 '17 at 21:07
  • I tried this code with a 25000 finished requests key and it doesn't work. Maybe gm_AuthFailure function doesn't recognise OverQueryLimit error. – Alex Aug 29 '17 at 18:40