最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js GoogleMaps API with @googlemapsjs-api-loader - Stack Overflow

programmeradmin1浏览0评论

Thanks for reading my question :)

I'm trying to implement GoogleMaps in my Vue.js project and using the @googlemaps/js-api-loader (from )

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>)?

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./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>)?

Share Improve this question asked Feb 19, 2022 at 0:27 TaramanTaraman 3153 silver badges9 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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
      })

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>

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
});

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>
发布评论

评论列表(0)

  1. 暂无评论