0

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.

user1560834
  • 151
  • 5
  • 16
  • You're mixing server-side and client-side events, I don't think this is ever going to work. What is the exact desired behaviour ? Actually I don't think you need your `OnTextChanged` event (which is probably causing the PostBack hence the page refresh). Should your client action need to update something server side, AJAX may be of help... – Laurent S. Nov 20 '13 at 16:15
  • The desired behaviour is that the latitude and longitude are fetched, put into their respective labels so they can be used, and the c# event is fired somehow. Doing it on the OnTextChanged is the only way I could think to do it. – user1560834 Nov 21 '13 at 09:25

1 Answers1

0

This post, albeit not directly related, may be of some use for you:

navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

Quoting the part that may be relevant to you:

'[...] the default timeout for getCurrentPosition is infinite(!). That means that your error handler will never be called if getCurrentPosition hangs somewhere on the back end.'

So just keep that in mind.

About the C# part, I don't know if you already have it up and running or not, so there's quite a few ways to do that. For example, an AJAX call to a handler page. Or, if you're using JQuery, a $.Get call. Or store the values on hidden fields, and later capture the values on your codebehind.

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46