CUTCODEDOWN
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 404 times)

AndrewTraub

  • Jr. Member
  • **
  • Posts: 80
  • 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: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

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
            e.preventDefault();
        }
    });
}
//fillInAddress function not relevant as the error occurs at new google.maps.places.Autocomplete(address1Field...


Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1054
  • Karma: +188/-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.
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

 

SMF spam blocked by CleanTalk

Advertisement