Reputation: 11
I've been trying to solve this for days but I think I'm failing due to my limited javascript knowledge.
The scenario:
I've got an ASP.Net page with a button on it (Visual Studio 2010 (VB))
On my aspx page I'm using jQuery
In the jQuery button click, I want to store the current location in local storage.
If we are online, I want to fire the server-side button click and pass the location to the code-behind page.
If we are offline, I want to do nothing (or store location if we haven't already stored it) but I want stop the server-side button from firing.
The next time the button is clicked, if we are online, I want to fire the server-side button click and pass the original location (from local storage) to the code-behind.
I've tried numerous way but having problems due to the 'firing-order' of navigator.geolocation.getCurrentPosition!
The problem is that the function parameter of getCurrentPosition 'fires' after my code has reached the point where I'm putting the stored location into my asp.net hidden control! At this point the local storage is empty. The local storage is filled after my code has run?
Hope I've explained myself!
Any help would be appreciated before I've torn all my hair out!
Striped down code in aspx page:
(code-behind page has a button called btnLocation (see below) that has a wired click event)
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#btnLocation").click(function () {
var savedLocation = localStorage.getItem('pos');
if (savedLocation == '') {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (pos) {
var myPos = pos.coords.latitude + "," + pos.coords.longitude;
localStorage.setItem('pos', myPos);
}, function () {
alert("Unable to find your location");
});
} else {
alert("Browser does not support GeoLocation! ");
}
}
if (navigator.onLine == true) {
$('#hdnLocation').val(localStorage.getItem('pos'));
localStorage.removeItem('pos');
return true;
} else {
return false;
}
});
});
</script>
Code-behind in aspx.vb:
Protected Sub btnClick(sender As Object, e As System.EventArgs) Handles btnLocation.Click
Dim ArrivedLocation As String = hdnLocation.Value.ToString.Trim
End Sub
Upvotes: 0
Views: 681
Reputation: 11
If anyone's interested I got round this with a 'workaround'. I'm sure there's a better way but...
I added a hidden button to my aspx page (btnLocation).
jQuery:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
// Class for storing location...
function ourLocation(theLongitude, theLatitude, theMessage) {
this.longitude = theLongitude,
this.latitude = theLatitude,
this.message = theMessage
};
// Main button click...
$("#btnArrived").click(function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(xLocation);
}
return false
});
// Function that returns coordinates from getCurrentPosition...
function xLocation(position) {
var myLocation = new ourLocation('', '', '');
var storedNameForLocation = 'arrived_location_' + jobNo + '_' + trade;
myPos = position;
myLocation.latitude = myPos.coords.latitude;
myLocation.longitude = myPos.coords.longitude;
myLocation.message = getCurrentTime();
localStorage.setItem(storedNameForLocation, JSON.stringify(myLocation));
$('#btnLocation').click();
}
// Hidden button click (that fires server-side or not)...
$("#btnLocation").click(function () {
if (navigator.onLine == true) {
var storedNameForLocation = 'arrived_location_' + jobNo + '_' + trade;
$('#hdnLocation').val(localStorage.getItem(storedNameForLocation));
return true;
} else {
return false;
}
});
});
</script>
And Server-side:
Protected Sub btnClick2(sender As Object, e As System.EventArgs) Handles btnLocation.Click
Dim ArrivedLocation As String = hdnLocation.Value.ToString.Trim
End Sub
At least it works!
Ted.
Upvotes: 1