-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapController.js
80 lines (64 loc) · 2.38 KB
/
MapController.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
import Message from "~/shared/messaging/Message";
import * as MapViewData from "~/shared/types/MapViewData";
const config = require("~/config.json");
class MapController {
constructor() {
this._mapId = null;
this._webViewMessenger = null;
this._wrldMap = null;
this._indoorControl = null;
this._mapUpdateInterval = null;
}
init(mapId, webViewMessenger) {
this._mapId = mapId;
this._webViewMessenger = webViewMessenger;
this._webViewMessenger.on("map_init", ({ data }) => { this._loadMap(data); });
this._webViewMessenger.on("map_setView", ({ data }) => { this._setView(data); });
}
_loadMap(options) {
this._wrldMap = L.Wrld.map(this._mapId, config.wrld_api_key, {
center: options.center,
headingDegrees: options.headingDegrees,
zoom: options.zoom,
indoorsEnabled: config.indoors_enabled
});
if (config.indoors_enabled) {
this._indoorControl = new WrldIndoorControl("wrld-indoor-control", this._wrldMap);
}
this._registerMapEvents();
}
_setView(data) {
const center = data.center;
let zoom = 16;
if (!center || !("lat" in center) || !("lng" in center)) {
window.alert("no valid center sent with map_setView message");
}
if ("zoom" in data) {
zoom = data.zoom;
}
this._wrldMap.indoors.exit();
this._wrldMap.setView(center, zoom);
}
_registerMapEvents() {
this._wrldMap.on("initialstreamingcomplete", (e) => { this._postMapEvent(e.type); });
this._wrldMap.on("movestart", () => { this._startIntervalMapUpdate(); });
this._wrldMap.on("moveend", () => { this._endIntervalMapUpdate(); });
}
_postMapEvent(type) {
const message = Message(type, MapViewData.fromWrldMap(this._wrldMap));
this._webViewMessenger.postMessage(message);
}
_postMapUpdateEvent() {
this._postMapEvent("mapviewupdate");
}
_startIntervalMapUpdate() {
this._postMapUpdateEvent();
this._mapUpdateInterval = setInterval(() => { this._postMapUpdateEvent(); }, 500);
}
_endIntervalMapUpdate() {
clearInterval(this._mapUpdateInterval);
this._postMapUpdateEvent();
}
}
const mapController = new MapController;
export default mapController;