I'm currently trying to implement a GeoLocation in my asp.net project. The code will get the user's location using the geolocation and update the latitude and longitude labels. Once the longitude label has been updated, it fires a C# event which will carry out an operation based on those values.
Only problem is, this seems to be happening too fast - and I don't know why! I'm also a bit stuck on devices to test it on, but that's another story. Long story short, on a mobile device the page asks if I want to grant it access to my location, but whilst it's doing that the page refreshes in the background.
Code is:
<asp:TextBox ID="lblLat" runat="server" BorderColor="White" BorderStyle="None" ForeColor="Black" Width="100px"></asp:TextBox>
<asp:TextBox ID="lblLon" runat="server" BorderColor="White" BorderStyle="None" ForeColor="Black" Width="100px" OnTextChanged="btnByProximity_Click"></asp:TextBox>
<button id="btnProximity" onclick="GetLocation()" runat="server" style="width: 30%">Proximity</button>
<script type="text/javascript">
function GetLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(ShowPosition, ShowError);
}
else { alert("Geolocation is not supported by this browser."); }
}
function ShowPosition(position)
{
document.getElementById("lblLat").value = position.coords.latitude;
document.getElementById("lblLon").value = position.coords.longitude;
//document.getElementById("lblChange").value = document.getElementById("lblChange").value + 1
}
function ShowError(error)
{
if (error.code == 1)
{
alert("User denied the request for Geolocation.")
}
else if (error.code == 2)
{
alert("Location information is unavailable.")
}
else if (error.code == 3)
{
alert("The request to get user location timed out.")
}
else
{
alert("An unknown error occurred.")
}
}
</script>
At the top we've got the two latitude and longitude labels, lblLat and lblLon. When lblLon is changed we fire the btnByProximity_Click event. Our btnProximity calls the GetLocation event which is supposed to get the location and update the lblLat and lblLon.