@fawmi/vue-google-maps

中文文档
0.9.79发布于 2022/08/30MITTypeScriptCommonJS近 30 天下载 14.8万

Google Map components for Vue.js 3

原始使用文档
文档目录14 节
  1. Documentation
  2. Installation
  3. Basic usage
  4. Configure Vue to use the Components
  5. Use it anywhere in your components
  6. Components
  7. Markers
  8. Cluster
  9. Heatmap
  10. Access map object
  11. Map options
  12. More components
  13. Nuxt 3 usage
  14. Contributions

Vue 3 Google maps Components

Set of mostly used Google Maps components for Vue.js.

Why this library exists?

We heavily use Google Maps in our projects, so I wanted to have a well maintained Google Maps library.

Documentation

Checkout vue-map.netlify.app for a detailed documentation or codesandbox for an example

Installation

You can install it using npm

npm install -S @fawmi/vue-google-maps

Basic usage

You need an API Key. Learn how to get an Api key .

Configure Vue to use the Components

In your main.js

import { createApp } from 'vue'
import  VueGoogleMaps from '@fawmi/vue-google-maps'

const app = createApp(App);
app.use(VueGoogleMaps, {
    load: {
        key: 'YOUR_API_KEY_COMES_HERE',
    },
}).mount('#app')

Use it anywhere in your components

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 100vw; height: 900px"
  >
  </GMapMap>
</template>

<script >
export default {
  name: 'App',
  data() {
    return {
      center: {lat: 51.093048, lng: 6.842120},
    }
  }
}
</script>

Components

Markers

If you need to add markers to the Map, add GMapMarker as child of GMapMap component.

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 500px; height: 300px"
  >
    <GMapMarker
        :key="marker.id"
        v-for="marker in markers"
        :position="marker.position"
    />
  </GMapMap>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      center: {lat: 51.093048, lng: 6.842120},
      markers: [
        {
          id: 'dfsldjl3r',
          position: {
            lat: 51.093048, lng: 6.842120
          },
        }
      ]
    }
  }
}
</script>

Cluster

If you have too many markers, it is helpful to cluster markers. You can easily cluster markers by wrapping your markers with GMapCluster component.

<template>
  <GMapMap
      :center="center"
      :zoom="7"
      map-type-id="terrain"
      style="width: 500px; height: 300px"
  >
    <GMapCluster>
      <GMapMarker
          :key="index"
          v-for="(m, index) in markers"
          :position="m.position"
          :clickable="true"
          :draggable="true"
          @click="center=m.position"
      />
    </GMapCluster>
  </GMapMap>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      center: {lat: 51.093048, lng: 6.842120},
      markers: [
        {
          position: {
            lat: 51.093048, lng: 6.842120
          },
        }
        , // Along list of clusters
      ]
    }
  }
}
</script>

Heatmap

If you need to add heatmap layer to the Map, add visualization library in load config and add GMapHeatmap as child of GMapMap component.

import { createApp } from 'vue'
import  VueGoogleMaps from '@fawmi/vue-google-maps'

const app = createApp(App);
app.use(VueGoogleMaps, {
  load: {
    key: 'YOUR_API_KEY_COMES_HERE',
    libraries: "visualization"
  },
}).mount('#app')

<template>
  <GMapMap
    ref="myMapRef"
    :center="center"
    :zoom="zoom"
    style="width: 100%; height: 600px"
  >
    <GMapHeatmap :data="heatData"></GMapHeatmap>
  </GMapMap>
</template>
<script>
export default {
  name: 'App',
  setup() {
    const center = {lat: 52.2985593, lng: 104.2455337}
    const zoom = 12
    const myMapRef = ref();
    const heatData = ref([])

    watch(myMapRef, googleMap => {
      if (googleMap) {
        googleMap.$mapPromise.then(map=> {
          heatData.value = [
            {location: new google.maps.LatLng({lat: 52.2985593, lng: 104.2455337})},
          ];
        })
      }
    });

    return {
      center,
      zoom,
      heatData,
      myMapRef
    }
  },
}
</script>

Checkout docs for more component

Access map object

If you want to access google map object, you can access it by getting ref of the map object.

<template>
  <GMapMap ref="myMapRef" />
</template>
<script>
export default {
  mounted() {
    console.log(this.$refs.myMapRef)
  }
}
</script>

Map options

You can pass Map options using options property:

See MapOptions for a complete list of available options.

<GMapMap
 :options="{
   zoomControl: true,
   mapTypeControl: false,
   scaleControl: false,
   streetViewControl: false,
   rotateControl: false,
   fullscreenControl: true,
   disableDefaultUi: false
 }"
>
</GMapMap>

More components

Many other components are also supported. Checkout docs for more.

Nuxt 3 usage

First add @fawmi/vue-google-maps to build.transpile property in your nuxt.config.ts.

export default defineNuxtConfig({
 build: {
   transpile: ['@fawmi/vue-google-maps']
 },
})

Then create a plugin ~/plugin/vueGoogleMaps.ts.

import { defineNuxtPlugin } from '#app'
import  VueGoogleMaps from '@fawmi/vue-google-maps'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(VueGoogleMaps, {
    load: {
      key: 'YOUR_GOOGLE_API_KEY',
    },
  })
})

Contributions

The best way to contribute is to report reproducible bugs, but feature requests and improvement suggestions are always welcome too. And definitely bug fixes and PR are welcome.

版本与发布记录

@fawmi/vue-google-maps 共发布 86 个版本,首次发布于 2020/10/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.9.79latest2022/08/304 年前
0.9.782022/08/304 年前
0.9.762022/08/304 年前
0.9.752022/08/304 年前
0.9.732022/08/304 年前
0.9.722022/06/144 年前

关于 @fawmi/vue-google-maps 的常见问题

@fawmi/vue-google-maps 是什么?

Google Map components for Vue.js 3

如何安装 @fawmi/vue-google-maps?

在项目目录运行 npm install @fawmi/vue-google-maps,或使用 pnpm add / yarn add / bun add。

@fawmi/vue-google-maps 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fawmi/vue-google-maps 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fawmi/vue-google-maps 可以商用吗?

@fawmi/vue-google-maps 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fawmi/vue-google-maps 的下载量有多少?

根据 npm 官方下载统计,@fawmi/vue-google-maps 在 2026/10/15 – 2026/11/13 这 30 天内被下载 148,036 次,日均约 4,935 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fawmi/vue-google-maps 还在维护吗?

最新版本 0.9.79 发布于 2022/08/30(4 年前);至今共发布 86 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/10/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。