-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
259 lines (234 loc) · 8.02 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/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.6.4/build/ol.js"></script>
</head>
<body>
<div id="map" class="map" style="max-width:800px"></div>
<div id="info" style="display: none;"></div>
<!-- <label for="track">
track position
<input id="track" type="checkbox"/>
</label>
-->
<p>
Tags:
<div id="geotags1"></div>
<div id="geotags2"></div>
<div id="geotags3"></div>
<div id="geotags4"></div>
</p>
<p>
position long : <code id="pos-lon"></code>
<br> position lat : <code id="pos-lat"></code> <br> position accuracy : <code id="accuracy"></code> <br> altitude : <code id="altitude"></code> <br> altitude accuracy : <code id="altitudeAccuracy"></code> <br> heading : <code id="heading"></code> <br> speed : <code id="speed"></code>
</p>
<script src="geofencing.js"></script>
<script>
var wgs84Proj = new ol.proj.Projection({ code : "EPSG:4326" });
var origProj = new ol.proj.Projection({ code : "EPSG:3857" });
var london = ol.proj.transform([-0.1256312, 51.5286883], wgs84Proj, origProj);
var view = new ol.View({
center: london,
zoom: 16
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: {
collapsible: false
}
}),
view: view
});
var geolocation = new ol.Geolocation({
projection: view.getProjection(),
tracking: true,
trackingOptions: {
enableHighAccuracy: true,
// Maximum age in milliseconds
maximumAge: 1000,
// Timeout in milliseconds. Default is 5
timeout: 5000
}
});
function el(id) {
return document.getElementById(id);
}
// update the HTML page when the position changes.
geolocation.on('change', function() {
console.log("Location changed");
var coordinates = geolocation.getPosition();
el('pos-lon').innerText = coordinates[0];
el('pos-lat').innerText = coordinates[1];
el('accuracy').innerText = geolocation.getAccuracy() + ' [m]';
el('altitude').innerText = geolocation.getAltitude() + ' [m]';
el('altitudeAccuracy').innerText = geolocation.getAltitudeAccuracy() + ' [m]';
el('heading').innerText = geolocation.getHeading() + ' [rad]';
el('speed').innerText = geolocation.getSpeed() + ' [m/s]';
});
// handle geolocation error.
geolocation.on('error', function(error) {
var info = document.getElementById('info');
info.innerHTML = error.message;
info.style.display = '';
});
var accuracyFeature = new ol.Feature();
geolocation.on('change:accuracyGeometry', function() {
accuracyFeature.setGeometry(geolocation.getAccuracyGeometry());
});
var positionFeature = new ol.Feature();
positionFeature.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: '#3399CC'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 2
})
})
}));
geolocation.on('change:position', function() {
if (geolocation.getAccuracy() < 50) {
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ?
new ol.geom.Point(coordinates) : null);
}
});
new ol.layer.Vector({
map: map,
source: new ol.source.Vector({
features: [accuracyFeature, positionFeature]
})
});
// Do some geofencing
// Camden Town hall coordinates
var polyCoord = [
{
"name": "Camden Town Hall",
"coordinates": [[-0.12591898441314694, 51.52897769699999],
[-0.1256453990936279, 51.52869068786285],
[-0.12476563453674312, 51.52904444305162],
[-0.12504994869232175, 51.529331449958704],
[-0.12591898441314694, 51.52897769699999]],
"function_inside": () => {
console.log("Camden!!");
el('geotags1').innerHTML = "Camden Town Hall";
},
"function_outside": () => {
el('geotags1').innerHTML = "";
}
},
{
"name": "Auditorium",
"coordinates": [[-0.1254254579544067, 51.52918127215159],
[-0.12511432170867917, 51.52890427623015],
[-0.1247763633728027, 51.52903776845085],
[-0.12504994869232175, 51.52932811267945],
[-0.1254254579544067, 51.52918127215159]],
"function_inside": () => {
console.log("Auditorium!");
// auditoriumFunction will only change the tag once, as it will only evaluate once
auditoriumFunction();
},
"function_outside": () => {
el('geotags2').innerHTML = "";
}
},
{
"name": "Small Park",
"coordinates": [[-0.12417018413543701, 51.52708540572499],
[-0.12396097183227538, 51.52669492313336],
[-0.12303292751312254, 51.526885158660434],
[-0.1230865716934204, 51.52697193249654],
[-0.12317776679992674, 51.52696859504434],
[-0.1233386993408203, 51.52733237589169],
[-0.12358546257019042, 51.527295664194924],
[-0.12354791164398193, 51.52723559044546],
[-0.12417018413543701, 51.52708540572499],
],
"function_inside": () => {
console.log("Small Park!");
el('geotags3').innerHTML = "You found the small park!";
},
},
{
"name": "Brunswick Square",
"coordinates": [[-0.12281298637390137, 51.52472577186194],
[-0.12251794338226318, 51.52422846425853],
[-0.12229800224304198, 51.52400150327881],
[-0.12187957763671872, 51.523854645571504],
[-0.1209515333175659, 51.5240248669613],
[-0.1214611530303955, 51.52493937882949],
[-0.12281298637390137, 51.52472577186194]],
"function_inside": () => {
console.log("Brunswick");
el('geotags4').innerHTML = "Brunswick Square";
},
"function_outside": () => {
el('geotags4').innerHTML = "";
}
}
]
// Create a polygon out of the coordinates
// Add the polygon to a collection of features
var polyFeatures = [];
var polygon;
for(var i = 0; i < polyCoord.length; i++) {
polygon = coordinatesToPolygon(polyCoord[i].coordinates, projection=origProj);
polyCoord[i].polygon = polygon;
polyFeatures.push(new ol.Feature({geometry: polygon}));
}
// Add the polygons to the map
new ol.layer.Vector({
map: map,
source: new ol.source.Vector({
features: polyFeatures,
})
});
// Update the geotag depending on location
geolocation.on('change:position', function() {
for(var i = 0; i < polyCoord.length; i++) {
// First work out whether the current user location is in the polygon
// This should probably also include a check for the accuracy of the location.
var pointInPoly = inside(geolocation.getPosition(), polyCoord[i].polygon.getCoordinates()[0]);
// This if should probably include a check to see if the previous point in the series of triggers has been checked.
// If we're in the zone, fire our function!
if(pointInPoly) {
var func = polyCoord[i].function_inside;
} else {
var func = polyCoord[i].function_outside;
}
if(func !== undefined) {
func();
};
}
});
// The function we call when we hit the auditorium. The meet of the function will only ever be run once - clever!
// This is a javascript closure.
var auditoriumFunction = (function() {
// Instead of having a variable that might be lost on page refresh, check local storage instead.
var evaluated = false;
return function() {
if(evaluated === false) {
// Now do whatever you want when you get to the zone.
el('geotags2').innerHTML = "You found the auditorium!";
console.log("Changing auditorium message");
// And don't forget to set evaluated to true so we don't go round again
evaluated = true;
}
}
})();
</script>
</body>
</html>