-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOSM.html
More file actions
405 lines (341 loc) · 11.7 KB
/
OSM.html
File metadata and controls
405 lines (341 loc) · 11.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<html>
<head>
<title>OL RADAR</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style type="text/css">
html,
body,
#map {
position: relative;
width: 100%;
height: 100%;
margin: 0;
}
</style>
<link rel="stylesheet" href="https://openlayers.org/en/v4.2.0/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.2.0/build/ol.js"></script>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<body onload="init();">
<div id="map" class="map">
<div id="popup"></div>
</div>
</body>
<script>
var rotation = 0;
var pos = {
lat: -34.397,
lng: 150.644
};
var zoom = 15;
var view = new ol.View({
center: ol.proj.transform([pos.lng, pos.lat], 'EPSG:4326', 'EPSG:3857'),
rotation: rotation,
zoom: zoom
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: document.getElementById('map'),
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
view: view
});
var markerLayer;
var currentLayer; //TODO: besser machen
function init() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(onPosChanged,
function() {
//TODO: handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
//TODO: handleLocationError(false, infoWindow, map.getCenter());
}
var element = document.getElementById('popup');
// Popup showing the position the user clicked
var popup = new ol.Overlay({
element: element,
positioning: 'bottom-center',
stopEvent: false,
offset: [0, -50]
});
map.addOverlay(popup);
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
if (feature) {
var coordinates = feature.getGeometry().getCoordinates();
popup.setPosition(coordinates);
$(element).popover({
'placement': 'top',
'html': true,
'content': feature.get('name')
});
$(element).popover('show');
} else {
$(element).popover('destroy');
}
});
}
function onPosChanged(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
// drawCurrentPos();
// drawKnownPlaces();
// nearbySearch();
/*
var markers = new OpenLayers.Layer.Markers("Markers");
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(position));
*/
drawCurrentPos();
drawKnownPlaces();
// map.setCenter(lonLat, zoom);
console.log("center" + [pos.lng, pos.lat]);
map.getView().setCenter(ol.proj.transform([pos.lng, pos.lat], 'EPSG:4326', 'EPSG:3857'));
}
function drawCurrentPos() {
if (currentLayer) {
map.removeLayer(currentLayer);
}
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([pos.lng, pos.lat], 'EPSG:4326', 'EPSG:3857')),
name: 'Hier bin ich',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon( /** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
currentLayer = vectorLayer;
map.addLayer(vectorLayer);
}
function distanceBetweenPoints(latlng1, latlng2) {
var line = new ol.geom.LineString([latlng1, latlng2]);
return line.getLength();
}
function getBounds() {
var extent = map.getView().calculateExtent(map.getSize());
extent = ol.proj.transformExtent(extent, 'EPSG:3857', 'EPSG:4326');
return extent;
}
async function createMarker(place, vs) {
//console.log("PLACE: ", place.name);
var bounds = await getBounds();
var home = [places[2].lng, places[2].lat];
var distance = distanceBetweenPoints(home, [place.lng, place.lat]);
console.log(distance);
if (!ol.extent.containsCoordinate(bounds, [place.lng, place.lat])) {
var padding = 0.002 - distance / 100;
var m = (place.lat - pos.lat) / (place.lng - pos.lng);
var placeLoc;
var x, y;
var compl = false;
bounds.maxx = bounds[2];
bounds.maxy = bounds[3];
bounds.minx = bounds[0];
bounds.miny = bounds[1];
if (bounds.maxx <= place.lng) {
y = m * (bounds.maxx - pos.lng) + pos.lat;
x = bounds.maxx - padding;
} else if (bounds.minx >= place.lng) {
y = m * (bounds.minx - pos.lng) + pos.lat;
x = bounds.minx + (padding);
}
if (y <= bounds.maxy && y >= bounds.miny) {
compl = true;
}
if ((!compl)) {
if (bounds.maxy <= place.lat) {
x = (bounds.maxy + m * pos.lng - pos.lat) / m;
y = bounds.maxy - padding;;
} else if (bounds.miny >= place.lat) {
x = (bounds.miny + m * pos.lng - pos.lat) / m;
y = bounds.miny + padding / 2;
}
}
placeLoc = {
lat: y,
lng: x
};
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([placeLoc.lng, placeLoc.lat], 'EPSG:4326', 'EPSG:3857')),
name: 'undefined',
population: 4000,
rainfall: 500
});
//console.log(distance, place.icon, 0.3 - distance);
var iconStyle = new ol.style.Style({
image: new ol.style.Icon( /** @type {olx.style.IconOptions} */ ({
//size: [50, 50],
scale: 0.3 - distance * 1.5,
src: place.icon
}))
});
iconFeature.setStyle(iconStyle);
vs.push(iconFeature);
}
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([place.lng, place.lat], 'EPSG:4326', 'EPSG:3857')),
name: 'undefined',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon( /** @type {olx.style.IconOptions} */ ({
//size: [50, 50],
scale: 0.1,
src: place.icon
}))
});
iconFeature.setStyle(iconStyle);
vs.push(iconFeature);
}
var places = [{
lng: 8.769332,
lat: 53.098145,
icon: './icons/landmark.png'
},
{
lng: 8.809172,
lat: 53.074748,
icon: './icons/start.png'
},
{
lng: 8.878000,
lat: 53.097930,
icon: './icons/home.png'
},
{
lng: 8.805053,
lat: 53.074786,
icon: './icons/finish.png'
}
];
async function drawKnownPlaces() {
await clearMarkers();
var vs = [];
for (var i = 0; i < places.length; i++) {
await createMarker(places[i], vs);
}
var vectorSource = new ol.source.Vector({
features: vs
});
vectorLayer = new ol.layer.Vector({
source: vectorSource
});
markerLayer = vectorLayer;
map.addLayer(vectorLayer);
}
function clearMarkers() {
if (markerLayer) {
map.removeLayer(markerLayer);
}
}
// browser agnostic orientation
function getBrowserOrientation() {
var orientation;
if (screen.orientation && screen.orientation.type) {
orientation = screen.orientation.type;
} else {
orientation = screen.orientation ||
screen.mozOrientation ||
screen.msOrientation;
}
/*
'portait-primary': for (screen width < screen height, e.g. phone, phablet, small tablet)
device is in 'normal' orientation
for (screen width > screen height, e.g. large tablet, laptop)
device has been turned 90deg clockwise from normal
'portait-secondary': for (screen width < screen height)
device has been turned 180deg from normal
for (screen width > screen height)
device has been turned 90deg anti-clockwise (or 270deg clockwise) from normal
'landscape-primary': for (screen width < screen height)
device has been turned 90deg clockwise from normal
for (screen width > screen height)
device is in 'normal' orientation
'landscape-secondary': for (screen width < screen height)
device has been turned 90deg anti-clockwise (or 270deg clockwise) from normal
for (screen width > screen height)
device has been turned 180deg from normal
*/
return orientation;
}
var defaultOrientation;
// called on device orientation change
function onHeadingChange(event) {
var heading = event.alpha;
if (event.webkitCompassHeading) {
heading = event.webkitCompassHeading; //iOS non-standard
}
var orientation = getBrowserOrientation();
if (typeof heading !== "undefined" && heading !== null) { // TODO && typeof orientation !== "undefined") {
// we have a browser that reports device heading and orientation
// what adjustment we have to add to rotation to allow for current device orientation
var adjustment = 0;
if (defaultOrientation === "landscape") {
adjustment -= 90;
}
if (typeof orientation !== "undefined") {
var currentOrientation = orientation.split("-");
if (defaultOrientation !== currentOrientation[0]) {
if (defaultOrientation === "landscape") {
adjustment -= 270;
} else {
adjustment -= 90;
}
}
if (currentOrientation[1] === "secondary") {
adjustment -= 180;
}
}
//deg to rad
rotation = (heading + adjustment) * Math.PI / 180;
view.setRotation(rotation);
drawKnownPlaces();
console.log(rotation);
console.log(heading);
}
}
window.addEventListener("deviceorientation", onHeadingChange);
var geolocation = new ol.Geolocation({
// take the projection to use from the map's view
projection: view.getProjection()
});
// listen to changes in position
geolocation.on('change', function(evt) {
console.log("POS EVENT: " + geolocation.getPosition());
});
</script>
</head>
</html>