项目作者: fawmi

项目描述 :
Reactive Vue 3 components for Google maps
高级语言: JavaScript
项目地址: git://github.com/fawmi/vue-google-maps.git
创建时间: 2020-10-16T14:42:00Z
项目社区:https://github.com/fawmi/vue-google-maps

开源协议:MIT License

下载


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

  1. 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

  1. import { createApp } from 'vue'
  2. import VueGoogleMaps from '@fawmi/vue-google-maps'
  3. const app = createApp(App);
  4. app.use(VueGoogleMaps, {
  5. load: {
  6. key: 'YOUR_API_KEY_COMES_HERE',
  7. },
  8. }).mount('#app')

Use it anywhere in your components

  1. <template>
  2. <GMapMap
  3. :center="center"
  4. :zoom="7"
  5. map-type-id="terrain"
  6. style="width: 100vw; height: 900px"
  7. >
  8. </GMapMap>
  9. </template>
  10. <script >
  11. export default {
  12. name: 'App',
  13. data() {
  14. return {
  15. center: {lat: 51.093048, lng: 6.842120},
  16. }
  17. }
  18. }
  19. </script>

Components

Markers

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

  1. <template>
  2. <GMapMap
  3. :center="center"
  4. :zoom="7"
  5. map-type-id="terrain"
  6. style="width: 500px; height: 300px"
  7. >
  8. <GMapMarker
  9. :key="marker.id"
  10. v-for="marker in markers"
  11. :position="marker.position"
  12. ></GMapMarker>
  13. </GMapMap>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'App',
  18. data() {
  19. return {
  20. center: {lat: 51.093048, lng: 6.842120},
  21. markers: [
  22. {
  23. id: 'dfsldjl3r',
  24. position: {
  25. lat: 51.093048, lng: 6.842120
  26. },
  27. }
  28. ]
  29. }
  30. }
  31. }
  32. </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.

  1. <template>
  2. <GMapMap
  3. :center="center"
  4. :zoom="7"
  5. map-type-id="terrain"
  6. style="width: 500px; height: 300px"
  7. >
  8. <GMapCluster>
  9. <GMapMarker
  10. :key="index"
  11. v-for="(m, index) in markers"
  12. :position="m.position"
  13. :clickable="true"
  14. :draggable="true"
  15. @click="center=m.position"
  16. ></GMapMarker>
  17. </GMapCluster>
  18. </GMapMap>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'App',
  23. data() {
  24. return {
  25. center: {lat: 51.093048, lng: 6.842120},
  26. markers: [
  27. {
  28. position: {
  29. lat: 51.093048, lng: 6.842120
  30. },
  31. }
  32. , // Along list of clusters
  33. ]
  34. }
  35. }
  36. }
  37. </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.

  1. import { createApp } from 'vue'
  2. import VueGoogleMaps from '@fawmi/vue-google-maps'
  3. const app = createApp(App);
  4. app.use(VueGoogleMaps, {
  5. load: {
  6. key: 'YOUR_API_KEY_COMES_HERE',
  7. libraries: "visualization"
  8. },
  9. }).mount('#app')
  1. <template>
  2. <GMapMap
  3. ref="myMapRef"
  4. :center="center"
  5. :zoom="zoom"
  6. style="width: 100%; height: 600px"
  7. >
  8. <GMapHeatmap :data="heatData"></GMapHeatmap>
  9. </GMapMap>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'App',
  14. setup() {
  15. const center = {lat: 52.2985593, lng: 104.2455337}
  16. const zoom = 12
  17. const myMapRef = ref();
  18. const heatData = ref([])
  19. watch(myMapRef, googleMap => {
  20. if (googleMap) {
  21. googleMap.$mapPromise.then(map=> {
  22. heatData.value = [
  23. {location: new google.maps.LatLng({lat: 52.2985593, lng: 104.2455337})},
  24. ];
  25. })
  26. }
  27. });
  28. return {
  29. center,
  30. zoom,
  31. heatData,
  32. myMapRef
  33. }
  34. },
  35. }
  36. </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.

  1. <template>
  2. <GMapMap ref="myMapRef" ></GMapMap>
  3. </template>
  4. <script>
  5. export default {
  6. mounted() {
  7. console.log(this.$refs.myMapRef)
  8. }
  9. }
  10. </script>

Map options

You can pass Map options using options property:

See MapOptions for a complete list of available options.

  1. <GMapMap
  2. :options="{
  3. zoomControl: true,
  4. mapTypeControl: false,
  5. scaleControl: false,
  6. streetViewControl: false,
  7. rotateControl: false,
  8. fullscreenControl: true,
  9. disableDefaultUi: false
  10. }"
  11. >
  12. </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.

  1. export default defineNuxtConfig({
  2. build: {
  3. transpile: ['@fawmi/vue-google-maps']
  4. },
  5. })

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

  1. import { defineNuxtPlugin } from '#app'
  2. import VueGoogleMaps from '@fawmi/vue-google-maps'
  3. export default defineNuxtPlugin((nuxtApp) => {
  4. nuxtApp.vueApp.use(VueGoogleMaps, {
  5. load: {
  6. key: 'YOUR_GOOGLE_API_KEY',
  7. },
  8. })
  9. })

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.