Skip to content

Commit

Permalink
Add real location tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Dec 30, 2023
1 parent 379bcc0 commit da72121
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/js/gpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function replayGPX(file, map, pointCallback, errorCallback, delayBetweenPoints =

// Actions to take when page is rendered in full
document.addEventListener('DOMContentLoaded', function () {
const inputElement = document.getElementById("gpxFileInput");
const locationProvider = createLocationProvider();
const audioQueue = createSpatialPlayer(locationProvider);
const announcer = createCalloutAnnouncer(audioQueue, proximityThresholdMeters, false);
Expand All @@ -80,6 +79,7 @@ document.addEventListener('DOMContentLoaded', function () {
map.plotPoints([{ latitude: latitude, longitude: longitude, heading: heading }], proximityThresholdMeters);
});

const inputElement = document.getElementById("gpxFileInput");
inputElement.addEventListener("change", function (event) {
const file = event.target.files[0];

Expand Down
26 changes: 23 additions & 3 deletions app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import { createSpatialPlayer } from './audio/sound.js'
import { createCalloutAnnouncer } from './audio/callout.js'
import { clearFeatureCache, clearURLCache } from './data/cache.js'
import { getLocation } from './spatial/geo.js';
import { getLocation, watchLocation } from './spatial/geo.js';
import { createLocationProvider } from './spatial/location.js'
import { createMap } from './spatial/map.js';

const proximityThresholdMeters = 500;
const proximityThresholdMeters = 100;

// Actions to take when page is rendered in full
document.addEventListener('DOMContentLoaded', function () {
Expand All @@ -26,12 +26,32 @@ document.addEventListener('DOMContentLoaded', function () {
});

// Hook up click event handlers
var btnCallouts = document.getElementById('btn_callouts');
var watchPositionHandler = null;
btnCallouts.addEventListener('click', function() {
if (watchPositionHandler) {
// Currently watching -- clear handler
navigator.geolocation.clearWatch(watchPositionHandler);
btnCallouts.textContent = 'Start';
audioQueue.stopAndClear();
audioQueue.addToQueue({ soundUrl: 'app/sounds/mode_exit.wav' });
} else {
// Not currently watching -- start handler
btnCallouts.textContent = 'Stop';

// play mode-enter sound
audioQueue.addToQueue({ soundUrl: 'app/sounds/mode_enter.wav' });

watchPositionHandler =watchLocation(locationProvider.update);
}
});

var btnNearMe = document.getElementById('btn_near_me');
btnNearMe.addEventListener('click', function() {
if (audioQueue.queue.length > 0) {
//if (btnNearMe.textContent == '(stop)') {
audioQueue.stopAndClear();
audioQueue.addToQueue({ soundUrl: 'app/sounds/mode_exit.wav', x: 0, y: 0 });
audioQueue.addToQueue({ soundUrl: 'app/sounds/mode_exit.wav' });
//btnNearMe.textContent = 'Places Near Me';
return;
}
Expand Down
22 changes: 22 additions & 0 deletions app/js/spatial/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ export function getLocation(callback) {
});
}

export function watchLocation(callback) {
return navigator.geolocation.watchPosition(
function (position) {
console.log(position);
callback(
position.coords.latitude,
position.coords.longitude,
position.coords.heading || 0, // not available on all platforms
);
},
function (error) {
// Reject the Promise with the error message
console.error("Error getting current position: " + error.message);
},
{
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0,
}
);
}

export function geoToXY(myLocation, myHeading, poiLocation) {
// Convert degrees to radians
const toRadians = degree => degree * (Math.PI / 180);
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script src="app/js/main.js" type="module"></script>
</head>
<body>
<button id="btn_callouts">Start</button>
<button id="btn_near_me">Places Near Me</button>
<button id="btn_clear">Clear Cached Data</button>

Expand Down

0 comments on commit da72121

Please sign in to comment.