0

I'm implementing an autocomplete input box using the Google place API. I understand that the API URL requires a callback function for initializing the map.

I'm referencing the place API URL in index.html header:

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />>
    <script async src="https://maps.googleapis.com/maps/api/js?key=[KEY]&libraries=places"></script>
    <title>DocBay</title>
  </head>

and initializing the API in the component onMount Hook

onMounted(() => {
  autocomplete.value = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), {
    componentRestrictions: { country: ['us'] },
    fields: ['address_components'],
    types: ['address']
  })
  autocomplete.value.addListener('place_changed', fillInAddress)
})  

This results in this error:

Loading the Google Maps JavaScript API without a callback is not supported

since I've to include the Google Maps Places API in the of my HTML and can't define the callback function in there, how do I get rid of this error?

0

1 Answer 1

0

According to the docs including a callback is mandatory - you can change your script tag to have the global function name like this:

<script>
   window.initMap = function () {
      ...
   };
</script>
<script async src=".../api/js?key=[KEY]&libraries=places&callback=initMap"></script> – 
Sign up to request clarification or add additional context in comments.

1 Comment

BTW, somewhere else in the docs they say you can omit the callback but it might be an outdated post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.