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
| import Feature from 'ol/Feature.js'; import Geolocation from 'ol/Geolocation.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import View from 'ol/View.js'; import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js'; import {OSM, Vector as VectorSource} from 'ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
const view = new View({ center: [0, 0], zoom: 2, });
const map = new Map({ layers: [ new TileLayer({ source: new OSM(), }), ], target: 'map', view: view, });
const geolocation = new Geolocation({ trackingOptions: { enableHighAccuracy: true, }, projection: view.getProjection(), });
function el(id) { return document.getElementById(id); }
el('track').addEventListener('change', function () { geolocation.setTracking(this.checked); });
geolocation.on('change', function () { 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]'; });
geolocation.on('error', function (error) { const info = document.getElementById('info'); info.innerHTML = error.message; info.style.display = ''; });
const accuracyFeature = new Feature(); geolocation.on('change:accuracyGeometry', function () { accuracyFeature.setGeometry(geolocation.getAccuracyGeometry()); });
const positionFeature = new Feature(); positionFeature.setStyle( new Style({ image: new CircleStyle({ radius: 6, fill: new Fill({ color: '#3399CC', }), stroke: new Stroke({ color: '#fff', width: 2, }), }), }) );
geolocation.on('change:position', function () { const coordinates = geolocation.getPosition(); positionFeature.setGeometry(coordinates ? new Point(coordinates) : null); });
new VectorLayer({ map: map, source: new VectorSource({ features: [accuracyFeature, positionFeature], }), });
|