Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Google Maps API - handling failure  (Read 331 times)


  • Jr. Member
  • **
  • Posts: 76
  • Karma: +0/-0
Google Maps API - handling failure
« on: 12 Aug 2021, 03:40:55 pm »
Does anyone have some experience on handling API failures with Google Maps API?

I have started work on a site where the API key validates and everything works fine on the server.  However, when running on localhost in Docker, the API fails because the key is invalid.  That wouldn't be a problem, except that the API screws up one of the entry fields with an error message and makes the field uneditable.

The easiest path I see would be to gracefully handle the error, but I cannot find a way to do that on google's docs.

My code is very similar to that found here:

Code: [Select]
"use strict";
let autocomplete;
let address1Field;
let address2Field;
let postalField;

function initAutocomplete() {
    address1Field = document.querySelector("#address");
    address2Field = document.querySelector("#address2");
    postalField = document.querySelector("#zip");
    // Create the autocomplete object, restricting the search predictions to
    // addresses in the US and Canada.
    autocomplete = new google.maps.places.Autocomplete(address1Field, {
        /*componentRestrictions: { country: ["us", "ca"] },*/
        fields: ["address_components", "geometry"],
        types: ["address"],
    // When the user selects an address from the drop-down, populate the
    // address fields in the form.
    autocomplete.addListener("place_changed", fillInAddress);
    google.maps.event.addDomListener(address1Field, 'keydown', function (e) {
        if (e.keyCode === 13) { //todo: revert back to == if enter doesn't work after testing
//fillInAddress function not relevant as the error occurs at new google.maps.places.Autocomplete(address1Field...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 919
  • Karma: +171/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Google Maps API - handling failure
« Reply #1 on: 13 Aug 2021, 01:56:23 am »
I'm not 100% sure about going that deep into Google maps, but I would think that if the token is rejected -- which should be done when you actually call google maps at the very start, you'd abort out THEN to completely avoid running ANY of the rest of your GM related code.

Basically you should be preventing what you've shown here from even running many, many, MANY steps before and layers above ... whatever the code you have here is trying to do.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.


SMF spam blocked by CleanTalk