Geolocation GetCurrentPosition vs WatchPosition (HTML5 JavaScript ASP.NET
MVC)
I'm creating some tracking functionality for a client who wants to be able
to see a driver's progress on a map for deliveries. I have a javascript
page in my APS.NET MVC4 application that has a one minue timer that gets
the mobile user's current location every minute then stores that value in
variables, then with a ajax makes a call into an MVC controller function
that saves that location data into a database. Then the location data can
be viewed on a bing map.
Did I implement this the correct way, or should I be using the HTML5
WatchPosition function instead of the GetCurrentPosition function? I did
not see a way to set an interval in the WatchPosition function, it seems
to fire every time the location changes which seems like it would be
continuously firing, I only need the location data every minute or so.
Thanks for your suggestions on my code below.
var Latitude = 0;
var Longitude = 0;
var TrackingLoop = setInterval(function () { GetCurrentLocation() },
60000);
function RestartTracking() { location.reload(); }
function GetCurrentLocation() {
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal,
maximumAge: 0 }
);
}
else {
alert("Geolocation is not supported by this browser");
}
}
function displayPosition(position) {
Latitude = position.coords.latitude;
Longitude = position.coords.longitude;
UpdateLocation(Latitude, Longitude);
}
function UpdateLocation(Latitude, Longitude) {
var JobID = document.getElementById("JobID").value;
$.ajax({
url: '/Job/UpdateLocation',
type: 'POST',
data: {
JobID: JobID,
Latitude: Latitude,
Longitude: Longitude
},
});
}
No comments:
Post a Comment