-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
openstreetmap_controller.js
81 lines (61 loc) 路 2.24 KB
/
openstreetmap_controller.js
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
'use strict';
import { Controller } from 'stimulus';
export default class extends Controller {
static values = {
latitude: Number,
longitude: Number,
zoom: Number,
height: Number,
title: Boolean,
};
connect() {
let position = [this.latitudeValue, this.longitudeValue];
let options = {
center: position,
zoom: this.zoomValue,
};
import('leaflet').then((L) => {
this.element.style.height = this.heightValue + 'px';
this.map = L.map(this.element, options);
const map = this.map;
let layer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(this.map);
import('leaflet-extra-markers').then((LM) => {
const icon = LM.ExtraMarkers.icon({
icon: 'fa-circle-o',
markerColor: 'blue',
shape: 'circle',
prefix: 'fa',
});
const markerOptions = {
icon: icon,
};
let marker = this._createMarker(L, position, markerOptions);
marker.addTo(map);
this._dispatchEvent('openstreetmap:addMarker', { map: map, layer: layer, marker: marker });
});
this._dispatchEvent('openstreetmap:connect', { map: map, layer: layer });
});
}
_createMarker(L, position, markerOptions) {
const marker = L.marker(position, markerOptions);
if (this.titleValue) {
const popup = L.popup().setContent(this.titleValue);
marker.bindPopup(popup).openPopup();
}
return marker;
}
disconnect() {
if (!this.map) {
return;
}
this.map.off();
this.map.remove();
}
_dispatchEvent(name, payload = null, canBubble = false, cancelable = false) {
const userEvent = document.createEvent('CustomEvent');
userEvent.initCustomEvent(name, canBubble, cancelable, payload);
this.element.dispatchEvent(userEvent);
}
}