Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Google maps component for vue with 2-way data binding

NotificationsYou must be signed in to change notification settings

xkjyeah/vue-google-maps

 
 

Repository files navigation

It's been increasingly difficult for me to make time to maintain this project.My projects at work have also gradually migrated away from Google Maps (but still on Vue -- Vue's awesome!),so there's less and less incentive to maintain.

If you have time to contribute to a rather frequently used library, feel free to make a PR!For more background, please refer tothis issue.

What's urgently needed are:

  1. Better automated tests
  2. Better integration tests with the popular frameworks, especially Nuxt and Vue template
  3. Better documentation (examples, recommendations)

The above three will go a long way to keeping the project maintainable and contributable, and will address many of the open issues.

vue-google-maps

Build Status

Vue-2 port of vue-google-maps

This is the Vue 2.x port of vue-google-maps!

Attention!

This great package doesn't release the last features added to it for different reasons, is because it that some developers land a new package with the last features added to it.

If you want to use the last features in this package you can usegmap-vue, it is a fork of this repository and has all new fixes and features added to it.

This new package has new documentation with live examples that can you test with your own gmap key. You can visit it following this linkgmap-vue docs.

Installation

With npm (Recommended)

npm install vue2-google-maps

Manually

Just downloaddist/vue-google-maps.js file and include it from your HTML.

Be aware that if you use this method, you cannot use TitleCase for your components and your attributes.That is, instead of writing<GmapMap>, you need to write<gmap-map>.

Example (Source code).

Basic usage / Documentation

Get an API key from Google

Generating an Google Maps API key.

Quickstart (Webpack, Nuxt):

If you are using Webpack and Vue file components, just add the following to your code!

<GmapMap  :center="{lat:10, lng:10}"  :zoom="7"  map-type-id="terrain"style="width:500px;height:300px">  <GmapMarker    :key="index"    v-for="(m, index) in markers"    :position="m.position"    :clickable="true"    :draggable="true"    @click="center=m.position"  /></GmapMap>

In yourmain.js or inside a Nuxt plugin:

importVuefrom'vue'import*asVueGoogleMapsfrom'vue2-google-maps'Vue.use(VueGoogleMaps,{load:{key:'YOUR_API_TOKEN',libraries:'places',// This is required if you use the Autocomplete plugin// OR: libraries: 'places,drawing'// OR: libraries: 'places,drawing,visualization'// (as you require)//// If you want to set the version, you can do so:// v: '3.26',},//// If you intend to programmatically custom event listener code//// (e.g. `this.$refs.gmap.$on('zoom_changed', someFunc)`)//// instead of going through Vue templates (e.g. `<GmapMap @zoom_changed="someFunc">`)//// you might need to turn this on.// autobindAllEvents: false,//// If you want to manually install components, e.g.//// import {GmapMarker} from 'vue2-google-maps/src/components/marker'//// Vue.component('GmapMarker', GmapMarker)//// then set installComponents to 'false'.//// If you want to automatically install all the components this property must be set to 'true':installComponents:true})

If you need to gain access to theMap instance (e.g. to callpanToBounds,panTo):

<template><GmapMapref="mapRef"...></GmapMap></template><script>exportdefault {mounted () {// At this point, the child GmapMap has been mounted, but// its map has not been initialized.// Therefore we need to write mapRef.$mapPromise.then(() => ...)this.$refs.mapRef.$mapPromise.then((map)=> {map.panTo({lat:1.38, lng:103.80})    })  }}

If you need to gain access to thegoogle object:

<template>  <GmapMarkerref="myMarker":position="google && new google.maps.LatLng(1.38, 103.8)" /></template><script>import {gmapApi}from'vue2-google-maps'exportdefault {  computed: {    google: gmapApi  }}</script>

Control the options of the map with the options property:

Example ofMapOptions:

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

Add region and language localization:

Example forLocalization:

Vue.use(VueGoogleMaps, {  load: {    region: 'VI',    language: 'vi',  },})

Nuxt.js config

For Nuxt.js projects, please import VueGoogleMaps in the following manner:

import*asVueGoogleMapsfrom'~/node_modules/vue2-google-maps'

Add the following to yournuxt.config.js'sbuild.extend():

transpile:[/^vue2-google-maps($|\/)/]

Officially supported components:

The list of officially support components are:

  • Rectangle, Circle
  • Polygon, Polyline
  • KML Layer
  • Marker
  • InfoWindow
  • Autocomplete
  • Cluster* (viamarker-clusterer-plus)

You can find examples of thison the website.Auto-generated API documentation for these components arehere.

ForCluster, youmust import the class specifically, e.g.

importGmapClusterfrom'vue2-google-maps/dist/components/cluster'// replace src with dist if you have Babel issuesVue.component('GmapCluster',GmapCluster)

Inconvenient, but this means all other users don't have to bundle the marker clusterer packagein their source code.

Autocomplete component

The autocomplete supports custom text field via scoped slot

<gmap-autocompleteclass="introInput"><templatev-slot:input="slotProps"><v-text-fieldoutlinedprepend-inner-icon="place"placeholder="Location Of Event"ref="input"v-on:listeners="slotProps.listeners"v-on:attrs="slotProps.attrs"></v-text-field></template></gmap-autocomplete>

The ref on the element must be called input, if the element is a vue component then it must have a child ref called input (like in vuetify text-field) or speciy a custom name via childRefName property (only works one level deep into a component).

The v-on:listeners is rquired, v-on:attrs may or may not be required depending on your implementation.

This requires vue 2.6 or higher for the new slot support.

NOTE: The official NPM package does not support this until the NPM package is updated, you can use this alternate temporary one or build your own version from source.

https://www.npmjs.com/package/vue2-google-maps-withscopedautocomp

Adding your own components

It should be relatively easy to add your own components (e.g. Heatmap, GroundOverlay). please refer to thesource code forMapElementFactory.

Example forDirectionsRenderer:

// DirectionsRenderer.jsimport{MapElementFactory}from'vue2-google-maps'exportdefaultMapElementFactory({name:'directionsRenderer',ctr:()=>google.maps.DirectionsRenderer,//// The following is optional, but necessary if the constructor takes multiple arguments//// e.g. for GroundOverlay// ctrArgs: (options, otherProps) => [options],events:['directions_changed'],// Mapped Props will automatically set up//   this.$watch('propertyName', (v) => instance.setPropertyName(v))//// If you specify `twoWay`, then it also sets up://   google.maps.event.addListener(instance, 'propertyName_changed', () => {//     this.$emit('propertyName_changed', instance.getPropertyName())//   })//// If you specify `noBind`, then neither will be set up. You should manually// create your watchers in `afterCreate()`.mappedProps:{routeIndex:{type:Number},options:{type:Object},panel:{},directions:{type:Object},//// If you have a property that comes with a `_changed` event,//// you can specify `twoWay` to automatically bind the event, e.g. Map's `zoom`:// zoom: {type: Number, twoWay: true}},// Any other properties you want to bind. Note: Must be in Object notationprops:{},// Actions you want to perform before creating the object instance using the// provided constructor (for example, you can modify the `options` object).// If you return a promise, execution will suspend until the promise resolvesbeforeCreate(options){},// Actions to perform after creating the object instance.afterCreate(directionsRendererInstance){},})

Thereafter, it's easy to use the newly-minted component!

<template>  <GmapMap:zoom="...":center="...">    <DirectionsRenderer />  </GmapMap></template><script>importDirectionsRendererfrom'./DirectionsRenderer.js'exportdefault {  components: {DirectionsRenderer}}</script>

Testing

More automated tests should be written to help new contributors.

Meanwhile, please test your changes against the suite ofexamples.

Improvements to the tests are welcome :)

About

Google maps component for vue with 2-way data binding

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript91.7%
  • HTML5.4%
  • Vue2.9%

[8]ページ先頭

©2009-2025 Movatter.jp