-
Notifications
You must be signed in to change notification settings - Fork 6
/
Vue2LeafletRotatedMarker.vue
124 lines (121 loc) · 2.78 KB
/
Vue2LeafletRotatedMarker.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<template>
<div style="display: none;">
<slot v-if="ready"></slot>
</div>
</template>
<script>
import { Icon, Marker, DomEvent } from 'leaflet'
import 'leaflet-rotatedmarker';
import { findRealParent, propsBinder } from 'vue2-leaflet';
const props = {
draggable: {
type: Boolean,
custom: true,
default: false,
},
visible: {
type: Boolean,
custom: true,
default: true,
},
latLng: {
type: [Object, Array],
custom: true,
},
icon: {
custom: false,
default: () => new Icon.Default(),
},
zIndexOffset: {
type: Number,
custom: false,
},
rotationAngle:{
type: Number,
default: () => 0,
},
rotationOrigin: {
type: String,
default: 'center center'
},
options: {
type: Object,
default: () => ({}),
},
};
export default {
name: 'LRotatedMarker',
props: props,
data() {
return {
ready: false,
}
},
mounted() {
const options = this.options;
if (this.icon) {
options.icon = this.icon;
}
options.draggable = this.draggable;
options.rotationAngle = this.rotationAngle?this.rotationAngle:0;
options.rotationOrigin = this.rotationOrigin;
this.mapObject = new Marker(this.latLng, options);
this.mapObject.on('move', (ev) => {
if (Array.isArray(this.latLng)) {
this.latLng[0] = ev.latlng.lat;
this.latLng[1] = ev.latlng.lng;
} else {
this.latLng.lat = ev.latlng.lat;
this.latLng.lng = ev.latlng.lng;
}
});
DomEvent.on(this.mapObject, this.$listeners);
propsBinder(this, this.mapObject, props);
this.ready = true;
this.parentContainer = findRealParent(this.$parent);
this.parentContainer.addLayer(this, !this.visible);
},
beforeDestroy() {
this.parentContainer.removeLayer(this);
},
watch: {
rotationAngle: {
handler: function () {
this.options.rotationAngle = this.rotationAngle
}
}
},
methods: {
setDraggable(newVal, oldVal) {
if (this.mapObject.dragging) {
newVal ? this.mapObject.dragging.enable() : this.mapObject.dragging.disable();
}
},
setVisible(newVal, oldVal) {
if (newVal == oldVal) return;
if (this.mapObject) {
if (newVal) {
this.parentContainer.addLayer(this);
} else {
this.parentContainer.removeLayer(this);
}
}
},
setLatLng(newVal) {
if (newVal == null) {
return;
}
if (this.mapObject) {
let oldLatLng = this.mapObject.getLatLng();
let newLatLng = {
lat: newVal[0] || newVal.lat,
lng: newVal[1] || newVal.lng,
};
if (newLatLng.lat != oldLatLng.lat || newLatLng.lng != oldLatLng.lng) {
this.mapObject.setLatLng(newLatLng);
}
}
}
}
};
</script>