4

Thanks for reading my question :)

I'm trying to implement GoogleMaps in my Vue.js project and using the @googlemaps/js-api-loader (from https://developers.google.com/maps/documentation/javascript/overview#javascript)

My Code looks as follows:

<script setup>
import { onMounted, ref } from 'vue'
import { Loader } from '@googlemaps/js-api-loader'

const loader = new Loader({
  apiKey: '',
  version: 'weekly',
  libraries: ['places']
})

const map = ref([])

onMounted(async () => {
  await loader
    .load()
    .then(google => {
      map.value = google.maps.Map(document.getElementById('map'), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8
      })
    })
    .catch(error => {
      console.log(error)
    })
    .then(function () {
    // always executed
    })
})
</script>

<template>
  <div
    id="map"
    class="map"
  />
</template>

<style>
  html,
  body,
  #map {
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>

But the map isn't showing, only that pop-up window, that the map can't load properly. In the console this error occurs: typeError: this.set is not a function. (In 'this.set("renderingType","UNINITIALIZED")', 'this.set' is undefined) Does anyone know how to get it running (at best in the <script setup>)?

4 Answers 4

2

Try to add new like this :

map.value = new google.maps.Map(document.getElementById('map'), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8
      })
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I thought I tried this already but obviously somehow different.
1

You must specify the width and height in the div with the id map

<template>
  <div class="map" id="map" style="width: 300px;height: 200px;" ></div>
</template>

Comments

0

try this too if it didn't work:

map.value = new window.google.maps.Map(document.getElementById('map'), {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 8
});

1 Comment

Please don't use code snippets for code that won't run.
0

You can try like below after installing @googlemaps/js-api-loader

package in your vuejs app .

  <template>
     <div id="map"></div>
   </template>

  <script setup>
    import { Loader } from "@googlemaps/js-api-loader";

    const {Map} = await loader.importLibrary("maps");

     onMounted(()=>{

        const map = new Map(document.getElementById("map"), {
            zoom: 4,
           center: { lat: -25.344, lng: 131.031 },
          });

    })
 </script>

Comments

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.