CONTRIBUTORS NEEDED!
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 to this issue.
What's urgently needed are:
- Better automated tests
- Better integration tests with the popular frameworks, especially Nuxt and Vue template
- 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
Vue-2 port of vue-google-maps
This is the Vue 2.x port of vue-google-maps!
Installation
With npm (Recommended)
npm install vue2-google-maps
Manually
Just download dist/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 your main.js
or inside a Nuxt plugin:
import Vue from 'vue'
import * as VueGoogleMaps from 'vue2-google-maps'
Vue.use(VueGoogleMaps, {
load: {
key: 'YOUR_API_TOKEN',
libraries: 'places',
},
})
If you need to gain access to the Map
instance (e.g. to call panToBounds
, panTo
):
<template>
<GmapMap ref="mapRef" ...>
</GmapMap>
</template>
<script>
export default {
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 the google
object:
<template>
<GmapMarker ref="myMarker"
:position="google && new google.maps.LatLng(1.38, 103.8)" />
</template>
<script>
import {gmapApi} from 'vue2-google-maps'
export default {
computed: {
google: gmapApi
}
}
</script>
Nuxt.js config
Add the following to your nuxt.config.js
's build.extend()
:
if (!isClient) {
config.externals.splice(0, 0, function (context, request, callback) {
if (/^vue2-google-maps($|\/)/.test(request)) {
callback(null, false)
} else {
callback()
}
})
}
In addition, for IE11 support, you will need the babel-polyfill
vendor:
build: {
vendors: ['babel-polyfill']
}
IE11 Support using the Vue CLI Tool
For IE11 support while using the Vue CLI tools you will need to add the following polyfills to the babel.config.js
file:
module.exports = {
presets: [
[
'@vue/app', {
polyfills: [
'es7.object.entries',
'es6.promise'
]
}
]
]
}
Officially supported components:
The list of officially support components are:
- Rectangle, Circle
- Polygon, Polyline
- Marker
- InfoWindow
- Autocomplete
- Cluster* (via
marker-clusterer-plus
)
You can find examples of this on the website.
Auto-generated API documentation for these components are here.
For Cluster
, you must import the class specifically, e.g.
import GmapCluster from 'vue2-google-maps/dist/components/cluster'
Vue.component('GmapCluster', GmapCluster)
Inconvenient, but this means all other users don't have to bundle the marker clusterer package
in their source code.
Adding your own components
It should be relatively easy to add your own components (e.g. Heatmap, GroundOverlay). please refer to the
source code for MapElementFactory
.
Example for DirectionsRenderer:
import {MapElementFactory} from 'vue2-google-maps'
export default MapElementFactory({
name: 'directionsRenderer',
ctr: () => google.maps.DirectionsRenderer,
events: ['directions_changed'],
mappedProps: {
routeIndex: { type: Number },
options: { type: Object },
panel: { },
directions: { type: Object },
},
props: {},
beforeCreate (options) {},
afterCreate (directionsRendererInstance) {},
})
Thereafter, it's easy to use the newly-minted component!
<template>
<GmapMap :zoom="..." :center="...">
<DirectionsRenderer />
</GmapMap>
</template>
<script>
import DirectionsRenderer from './DirectionsRenderer.js'
export default {
components: {DirectionsRenderer}
}
</script>
Testing
More automated tests should be written to help new contributors.
Meanwhile, please test your changes against the suite of examples.
Improvements to the tests are welcome :)