Skip to content

Commit b027e2f

Browse files
committed
add events for GeolocateControl
1 parent 99e9510 commit b027e2f

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

index.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929
}"
3030
:geolocate-control="{
3131
show: true,
32-
position: 'top-left'
32+
position: 'top-left',
33+
options: {
34+
positionOptions: {
35+
enableHighAccuracy: true
36+
},
37+
trackUserLocation: true
38+
}
3339
}"
3440
:scale-control="{
3541
show: true,
@@ -42,10 +48,15 @@
4248
@map-init="mapInit"
4349
@map-load="mapLoaded"
4450
@map-click="mapClicked"
45-
@map-mousemove="mapMouseMoved">
51+
@map-mousemove="mapMouseMoved"
52+
@geolocate-geolocate="geolocate"
53+
@geolocate-error="geolocateError"
54+
@geolocate-trackuserlocationstart="geolocateStart"
55+
@geolocate-trackuserlocationend="geolocateEnd"
56+
>
4657
</mapbox>
4758
</div><!-- /#app -->
4859
<script src="https://unpkg.com/vue"></script>
49-
<script src="/dist/app.js"></script>
60+
<script src="dist/app.js"></script>
5061
</body>
5162
</html>

readme.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,17 @@ access-token="your access token"
170170

171171
All Mapbox GL JS events are available for use. For a list of events look here: [https://www.mapbox.com/mapbox-gl-js/api/#map.event:resize](https://www.mapbox.com/mapbox-gl-js/api/#map.event:resize)
172172

173-
Events can be used by prepending `map-` to the beginning of the Mapbox event. For example for the Mapbox `click` event `@map-click` can be used.
173+
Map and control events can be used by adding a prefix introduced in the following table to the beginning of the Mapbox event name.
174+
175+
| Object | prefix |
176+
| ----- | ------ |
177+
| Map | `map-` |
178+
| GeolocateControl | `geolocate-` |
179+
180+
For example for the Mapbox Map `click` event `@map-click` can be used and for the GeolocateControl geolocate event `@geolocate-geolocate` can be used.
181+
182+
All events are passed the `map` or `control` object and the event if it has one.
174183

175-
All events are passed the `map` object and the event if it has one.
176184

177185
Example:
178186

@@ -183,6 +191,7 @@ HTML File:
183191
<mapbox
184192
@map-load="mapLoaded"
185193
@map-click="mapClicked"
194+
@geolocate-error="geolocateError"
186195
>
187196
</mapbox>
188197
</div>
@@ -238,6 +247,9 @@ const app = new Vue({
238247
mapClicked(map, e) {
239248
alert('Map Clicked!');
240249
},
250+
geolocateError(control, positionError) {
251+
alert('Error in locating:' + positionError);
252+
}
241253
}
242254
});
243255
```

src/app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ const app = new Vue({
5656
const features = map.queryRenderedFeatures(e.point, { layers: ['points'] });
5757
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
5858
},
59+
geolocate(control, position) {
60+
console.log('User position:' + position);
61+
},
62+
geolocateError(control, positionError) {
63+
console.log('Position Error:' + positionError);
64+
},
65+
geolocateStart(control) {
66+
console.log('geolocate started');
67+
},
68+
geolocateEnd(control) {
69+
console.log('geolocate ended');
70+
},
5971
addPopUp(map, e) {
6072
const features = map.queryRenderedFeatures(e.point, { layers: ['points'] });
6173
if (!features.length) {

src/components/Mapbox.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@
316316
if (this.geolocateControl.show) {
317317
const geolocate = new mapboxgl.GeolocateControl(this.geolocateControl.options);
318318
map.addControl(geolocate, this.geolocateControl.position);
319+
320+
geolocate.on('geolocate', position => {
321+
this.$emit('geolocate-geolocate', geolocate, position);
322+
});
323+
324+
geolocate.on('trackuserlocationstart', () => {
325+
this.$emit('geolocate-trackuserlocationstart', geolocate);
326+
});
327+
328+
geolocate.on('trackuserlocationend', () => {
329+
this.$emit('geolocate-trackuserlocationend', geolocate);
330+
});
331+
332+
geolocate.on('error', positionError => {
333+
this.$emit('geolocate-error', geolocate, positionError);
334+
});
319335
}
320336
321337
//Scale Control

0 commit comments

Comments
 (0)