Skip to content

Commit 8296d74

Browse files
authored
Add example for creating custom controls
1 parent a23f545 commit 8296d74

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# How to add custom controls
2+
3+
[Interactive example on Condesandbox](https://stackblitz.com/edit/vue-google-maps-marker-ry3zf7?file=src/components/ComponentWithMap.vue)
4+
5+
6+
To add custom controls, you can access google maps object and add controls to it, look at this example or follow the interactive example on condesandbox above.
7+
8+
9+
## Example
10+
11+
```bash
12+
<template>
13+
<GMapMap
14+
:center="center"
15+
ref="myMapRef"
16+
:zoom="10"
17+
map-type-id="terrain"
18+
style="width: 100vw; height: 20rem"
19+
>
20+
<GMapCluster :zoomOnClick="true">
21+
<GMapMarker
22+
:key="index"
23+
v-for="(m, index) in markers"
24+
:position="m.position"
25+
:clickable="true"
26+
:draggable="true"
27+
@click="center = m.position"
28+
/>
29+
</GMapCluster>
30+
</GMapMap>
31+
</template>
32+
33+
<script>
34+
export default {
35+
mounted() {
36+
this.$refs.myMapRef.$mapPromise.then((map) => {
37+
// Create the DIV to hold the control and call the CenterControl()
38+
// constructor passing in this DIV.
39+
const centerControlDiv = document.createElement('div');
40+
this.addCenterControl(centerControlDiv, map);
41+
map.controls[google.maps.ControlPosition.TOP_CENTER].push(
42+
centerControlDiv
43+
);
44+
});
45+
},
46+
methods: {
47+
addCenterControl(controlDiv, map) {
48+
const controlUI = document.createElement('div');
49+
50+
controlUI.innerHTML = `
51+
<div style="color: white; background: red; padding: 1rem;">
52+
You can click me, i can also be a vue component
53+
</div>
54+
`;
55+
56+
controlDiv.appendChild(controlUI);
57+
controlUI.addEventListener('click', () => {
58+
// Do what ever you want to happen on click event
59+
map.setCenter({
60+
lat: 53.57532,
61+
lng: 10.01534,
62+
});
63+
});
64+
},
65+
},
66+
data() {
67+
return {
68+
center: { lat: 51.093048, lng: 6.84212 },
69+
markers: [
70+
{
71+
position: {
72+
lat: 51.093048,
73+
lng: 6.84212,
74+
},
75+
},
76+
{
77+
position: {
78+
lat: 51.198429,
79+
lng: 6.69529,
80+
},
81+
},
82+
{
83+
position: {
84+
lat: 51.165218,
85+
lng: 7.067116,
86+
},
87+
},
88+
{
89+
position: {
90+
lat: 51.09256,
91+
lng: 6.84074,
92+
},
93+
},
94+
],
95+
};
96+
},
97+
};
98+
</script>
99+
100+
<style>
101+
body {
102+
margin: 0;
103+
}
104+
</style>
105+
```

0 commit comments

Comments
 (0)