-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.html
163 lines (135 loc) · 4.6 KB
/
viewer.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<script src='https://unpkg.com/[email protected]/dist/maplibre-gl.js'></script>
<link href='https://unpkg.com/[email protected]/dist/maplibre-gl.css' rel='stylesheet'/>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 25%;
bottom: 0;
width: 100%;
}
</style>
<style>
#myDropdown {
width: 200px;
padding: 10px;
}
</style>
</head>
<body>
<style>
.marker {
display: block;
border: none;
border-radius: 50%;
cursor: pointer;
padding: 0;
}
</style>
<h1>Where am I in Sapporo</h1>
<h2>Your ID: <span id="ws-id"></span></h2>
<form action="" onsubmit="sendMessage(event)">
<select id="myDropdown">
<option value="">Choose My Location</option>
<option value="option1">テレビ塔</option>
<option value="option2">時計台</option>
<option value="option3">札幌駅</option>
<option value="option4">Associeレンタルスペース</option>
<option value="option5">中島公園</option>
<option value="option6">近代美術館</option>
</select>
<button>Send</button>
</form>
<ul id='messages'>
</ul>
<div id='map'></div>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<script>
var loc_coordinat = {
"option1": "141.3538497,43.0611086",
"option2": "141.353556,43.062558",
"option3": "141.35113642643597,43.0686946023698",
"option4": "141.3576215,43.06454",
"option5": "141.35451081847282,43.04515309937545",
"option6": " 141.33038906779382,43.060588292260945"
}
const map = new maplibregl.Map({
container: 'map',
style: {
"version": 8,
"sources": {
"osm": {
"type": "raster",
"tiles": ["https://a.tile.openstreetmap.org/{z}/{x}/{y}.png"],
"tileSize": 256,
"attribution": "© OpenStreetMap Contributors",
"maxzoom": 19
}
},
"layers": [
{
"id": "osm",
"type": "raster",
"source": "osm" // This must match the source key above
}
]
},
center: [141.34867329542533, 43.0598172943666], // starting position [lng, lat]
zoom: 13 // starting zoom
});
map.addControl(new maplibregl.NavigationControl());
var points = [];
let client_id = Date.now();
document.querySelector("#ws-id").textContent = client_id;
let hostName = location.hostname;
let ws = new WebSocket(`wss://${hostName}/ws/${client_id}`);
// var ws = new WebSocket(`ws://localhost:8000/ws/${client_id}`);
ws.onmessage = function (event) {
// add markers to map
Object.keys(JSON.parse(event.data).markers).forEach((key, index) => {
let coordinate = JSON.parse(event.data).markers[key].coordinate;
let marker_size = JSON.parse(event.data).markers[key].marker_size;
if (document.getElementById(key)) {
document.getElementById(key).remove();
}
let el = document.createElement('div');
el.id = key;
el.className = 'marker';
el.style.backgroundImage =
'url(https://placekitten.com/g/' +
marker_size + '/' + marker_size + '/)';
console.log('url(https://placekitten.com/g/' +
marker_size + '/' + marker_size + '/)');
el.style.width = marker_size + 'px';
el.style.height = marker_size + 'px';
el.addEventListener('click', function () {
window.alert(key);
});
// add marker to map
new maplibregl.Marker(el)
.setLngLat(coordinate.split(','))
.addTo(map);
points.push(coordinate.split(','));
// Create a GeoJSON FeatureCollection of points
const pointsCollection = turf.featureCollection(points.map(point => turf.point(point)));
const bbox = turf.bbox(pointsCollection);
map.fitBounds([
[bbox[0], bbox[1]],
[bbox[2], bbox[3]]
]);
});
};
function sendMessage(event) {
ws.send(loc_coordinat[document.getElementById("myDropdown").value])
event.preventDefault()
}
</script>
</body>
</html>