-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mapbox.vue
71 lines (63 loc) · 1.82 KB
/
Mapbox.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<template>
<div class="mapbox">
<link
href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css"
rel="stylesheet"
/>
<div id="mapContainer" />
</div>
</template>
<script lang="ts">
// Imports
import { Vue, Options } from "vue-class-component";
import { Prop, Watch } from "vue-property-decorator";
import mapboxgl from "mapbox-gl";
// Component options
@Options({
components: { mapboxgl }
})
// Component
export default class MapBoxComponent extends Vue {
// Props
@Prop({ required: true }) private accessToken!: string;
@Prop({ required: true, default: 52.132633 }) private latitude!: number;
@Prop({ required: true, default: 5.291266 }) private longitude!: number;
@Prop({ required: false, default: 4 }) private zoom!: number;
@Prop({ required: false, default: true }) private keepCentered!: boolean;
// Class variables
private map!: mapboxgl.Map;
private marker: mapboxgl.Marker = new mapboxgl.Marker();
// Lifecycle hooks
mounted() {
this.map = new mapboxgl.Map({
accessToken: this.accessToken,
container: "mapContainer",
style: "mapbox://styles/ricardobalk/ciyokz20q00382smtd67whbog",
center: [5.291266, 52.132633], // Center of NL
zoom: this.zoom
});
}
// Watchers
@Watch("latitude")
@Watch("longitude")
onCoordsChanged() {
if (this.keepCentered) {
this.map.setCenter([this.longitude, this.latitude]);
}
this.marker.setLngLat([this.longitude, this.latitude]).addTo(this.map);
}
@Watch("keepCentered")
onKeepCenteredChanged() {
if (this.keepCentered) {
this.map.setCenter([this.longitude, this.latitude]);
}
}
}
</script>
<style lang="stylus">
.mapbox
height 100%
#mapContainer {
height 100%
}
</style>