如何使用 `new google.maps.Marker()` 和 `vue2-google-maps`

How to use `new google.maps.Marker()` with `vue2-google-maps`

由于某些原因,我必须使用 new google.maps.Marker()vue2-google-maps,但我不知道如何开始,因为文档和许多人在 [=] 中使用 <GmapMarker ... /> 24=] 部分代替。

我试过搜索,但文档没有涵盖这个。

现在我的代码看起来像这样,它没有任何错误,但它也不起作用(地图存在但标记未显示):

<template>
  <div style="height: 100%; width: 100%;">
    <GmapMap
      :center="{ lat: 0, lng: 0 }"
      ref="mapRef"
    >
    </GmapMap>
  </div>
</template>
<script>
import * as VueGoogleMaps from 'vue2-google-maps';

export default {
  computed: {
    google: VueGoogleMaps.gmapApi,
  },
  methods: {
    init() {
      new this.google.maps.Marker({
        position: {
          lat: 0,
          lng: 0
        },
        map: this.$refs.mapRef,
      });
    }
  },
  async mounted() {
    this.init()
  },
}
</script>

好的,我明白了。这是我所做的:


data()

中创建地图变量
data() {
  return {
    map: undefined,
  }
},

使用mounted()

中的函数设置
mounted() {
  this.$refs.mapRef.$mapPromise.then((map) => {
    this.map = map;
    this.init();
  });
},

现在,Bob 是你的叔叔,你可以随心所欲地使用 this.map

methods: {
  init() {
    new this.google.maps.Marker({
      position: {
        lat: 0,
        lng: 0
      },
      map: this.map,
    });
  },
}