I'm encountering an issue with this JavaScript code. The second alert in the (tracking_data) is displaying 'undefined', as if the variable had been deleted or cleared, but I cannot find any part of the code that would cause that. Any assistance you could provide would be greatly appreciated. Regards.
var track_id = ''; // Name/ID of the exercise
var watch_id = null; // ID of the geolocation
var tracking_data = []; // Array containing GPS position objects
$("#startTracking_start").on('click', function () {
// Start tracking the User
watch_id = navigator.geolocation.watchPosition(
// Success
function (position) {
tracking_data.push(position);
alert(tracking_data);
},
// Error
function (error) {
console.log(error);
},
// Settings
{frequency: 3000, enableHighAccuracy: true });
// Tidy up the UI
track_id = $("#track_id").val();
$("#track_id").hide();
$("#startTracking_status").html("Tracking workout: <strong>" + track_id + "</strong>");
});
$("#startTracking_stop").on('click', function(){
alert(tracking_data);
// Stop tracking the user
navigator.geolocation.clearWatch(watch_id);
// Save the tracking data
window.localStorage.setItem(track_id, JSON.stringify(tracking_data));
// Reset watch_id and tracking_data
watch_id = null;
tracking_data = [];
// Tidy up the UI
$("#track_id").val("").show();
$("#startTracking_status").html("Stopped tracking workout: <strong>" + track_id + "</strong>");
});