google-maps-api-3vuejs3google-places-api

Google Places API (New) Autocomplete not working


I am trying to setup a map using the Places Autocomplete service so I can type an address and plot it on a map. I am using the @googlemaps/js-api-loader npm package to load.

The version when using Loader has a different issue/error for each. I've only seen the Autocomplete kind of work on the alpha version and I am using the example from the Google API docs.

I've also verified that the URL generated by the js-api-loader is the same as the example on this page. They are explicitly using the beta version, but I am getting an API error that it's not enabled. However the fact that it works on the alpha version makes me question that error. But just in case I have included a screenshot of the APIs enabled for the API_KEY I am using here. I also tried it with "Don't restrict key" either. That fails as well. This page also tells me I should be using the beta version, but it's rejecting my API calls saying I haven't enabled the API (screenshot below disagrees).

Code is as follows (vue3):

import { Loader } from '@googlemaps/js-api-loader';

const loader = new Loader({
    apiKey: 'MY_API_KEY',
    version: 'alpha', // weekly, alpha or beta (none work completely)
    libraries: ['places']
});

async function initMap() {
    // Request needed libraries.
    const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([
        loader.importLibrary("marker"),
        loader.importLibrary("places"),
    ]);
    // Initialize the map.
    map.value = new google.maps.Map(document.getElementById("map"), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 9,
        mapId: "4504f8b37365c3d0",
        mapTypeControl: false,
    });

    let placeAutocomplete =
        new google.maps.places.PlaceAutocompleteElement({
            componentRestrictions: {country: ['us']},
        });
    placeAutocomplete.id = "pac-input";
    let card = document.getElementById("pac-map");
    card.appendChild(placeAutocomplete);

    placeAutocomplete.addEventListener('gmp-placeselect', async ({ place }) => {
        console.log(place);  // NEVER MAKES IT HERE IN ALPHA
    });
}

onMounted(async () => {
    initMap();
});

enter image description here


Solution

  • A couple of things seem to be going on here:

    As for your code-example: