Skip to content

Commit e930f7b

Browse files
author
Peter Hegman
committed
Merge branch 'LKajan-control_events'
2 parents 99e9510 + d46cad3 commit e930f7b

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed

dist/Mapbox.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ exports.default = {
397397
});
398398
},
399399
addControls: function addControls(map) {
400+
var _this2 = this;
401+
400402
//Nav Control
401403
if (this.navControl.show) {
402404
var nav = new mapboxgl.NavigationControl();
@@ -407,6 +409,22 @@ exports.default = {
407409
if (this.geolocateControl.show) {
408410
var geolocate = new mapboxgl.GeolocateControl(this.geolocateControl.options);
409411
map.addControl(geolocate, this.geolocateControl.position);
412+
413+
geolocate.on('geolocate', function (position) {
414+
_this2.$emit('geolocate-geolocate', geolocate, position);
415+
});
416+
417+
geolocate.on('trackuserlocationstart', function () {
418+
_this2.$emit('geolocate-trackuserlocationstart', geolocate);
419+
});
420+
421+
geolocate.on('trackuserlocationend', function () {
422+
_this2.$emit('geolocate-trackuserlocationend', geolocate);
423+
});
424+
425+
geolocate.on('error', function (positionError) {
426+
_this2.$emit('geolocate-error', geolocate, positionError);
427+
});
410428
}
411429

412430
//Scale Control

dist/app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ exports.default = {
440440
});
441441
},
442442
addControls: function addControls(map) {
443+
var _this2 = this;
444+
443445
//Nav Control
444446
if (this.navControl.show) {
445447
var nav = new mapboxgl.NavigationControl();
@@ -450,6 +452,22 @@ exports.default = {
450452
if (this.geolocateControl.show) {
451453
var geolocate = new mapboxgl.GeolocateControl(this.geolocateControl.options);
452454
map.addControl(geolocate, this.geolocateControl.position);
455+
456+
geolocate.on('geolocate', function (position) {
457+
_this2.$emit('geolocate-geolocate', geolocate, position);
458+
});
459+
460+
geolocate.on('trackuserlocationstart', function () {
461+
_this2.$emit('geolocate-trackuserlocationstart', geolocate);
462+
});
463+
464+
geolocate.on('trackuserlocationend', function () {
465+
_this2.$emit('geolocate-trackuserlocationend', geolocate);
466+
});
467+
468+
geolocate.on('error', function (positionError) {
469+
_this2.$emit('geolocate-error', geolocate, positionError);
470+
});
453471
}
454472

455473
//Scale Control
@@ -539,6 +557,18 @@ var app = new Vue({
539557
var features = map.queryRenderedFeatures(e.point, { layers: ['points'] });
540558
map.getCanvas().style.cursor = features.length ? 'pointer' : '';
541559
},
560+
geolocate: function geolocate(control, position) {
561+
console.log('User position: ' + position.coords.latitude + ', ' + position.coords.longitude);
562+
},
563+
geolocateError: function geolocateError(control, positionError) {
564+
console.log(positionError);
565+
},
566+
geolocateStart: function geolocateStart(control) {
567+
console.log('geolocate started');
568+
},
569+
geolocateEnd: function geolocateEnd(control) {
570+
console.log('geolocate ended');
571+
},
542572
addPopUp: function addPopUp(map, e) {
543573
var features = map.queryRenderedFeatures(e.point, { layers: ['points'] });
544574
if (!features.length) {

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: 18 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 or position object 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,8 @@ HTML File:
183191
<mapbox
184192
@map-load="mapLoaded"
185193
@map-click="mapClicked"
194+
@geolocate-error="geolocateError"
195+
@geolocate-geolocate="geolocate"
186196
>
187197
</mapbox>
188198
</div>
@@ -238,6 +248,12 @@ const app = new Vue({
238248
mapClicked(map, e) {
239249
alert('Map Clicked!');
240250
},
251+
geolocateError(control, positionError) {
252+
console.log(positionError);
253+
},
254+
geolocate(control, position) {
255+
console.log(`User position: ${position.coords.latitude}, ${position.coords.longitude}`);
256+
}
241257
}
242258
});
243259
```

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.coords.latitude}, ${position.coords.longitude}`);
61+
},
62+
geolocateError(control, positionError) {
63+
console.log(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)