我在使用这个 javascript 代码时遇到问题,第二次警报 (tracking_data) 会抛出“未定义”,就像变量被删除或清除一样,我没有看到代码的任何部分这样做。我希望你能帮我解决这个问题。问候。
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").live('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").live('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
var watch_id = null;
var tracking_data = null;
// Tidy up the UI
$("#track_id").val("").show();
$("#startTracking_status").html("Stopped tracking workout: <strong>" + track_id + " </strong>");
});