0

The project I'm working on uses smartphones with Android (can't tell what version but it's recent) to find the user's current position. However, at the moment, it brings up the "share current position" allow/reject question, but then does nothing. If you leave it on that then the page in the background reloads after a bit, and if you allow it then it reloads in any case. It should bring up the location in a message prompt.

I will point out that this works on an iPhone I've had access to, and in IE10 (I think it is).

<asp:Button ID="btnLocate" Text="Loc" OnClientClick="return GetLocation()" runat="server" />

<script type="text/javascript">

            function GetLocation()
            {
                if (navigator.geolocation)
                {
                    navigator.geolocation.getCurrentPosition(ShowPosition, ShowError, { enableHighAccuracy: true, timeout: 31000, maximumAge: 90000 });
                }
                else{alert("Geolocation is not supported by this browser.");}
            }
            function ShowPosition(position)
            {
                alert(position.coords.latitude + "," + position.coords.longitude);
                return false;
            }
            function ShowError(error)
            {
                switch(error.code) 
                {
                    case error.PERMISSION_DENIED:
                        alert("User denied the request for Geolocation.");
                        break;
                    case error.POSITION_UNAVAILABLE:
                        alert("Location information is unavailable.");
                        break;
                    case error.TIMEOUT:
                        alert("The request to get user location timed out.");
                        break;
                    case error.UNKNOWN_ERROR:
                        alert("An unknown error occurred.");
                        break;
                }
            }
        </script>

I should also mention that GPS is enabled and Google Maps is able to find the location just-about instantly.

16
  • If you set enableHighAccuracy to false, does it work? Commented Apr 14, 2014 at 11:14
  • Nope, given that a go (as well as increased the timeout). It basically seems like it must refresh after a few seconds, regardless of if the allow button is pressed or not. Commented Apr 14, 2014 at 11:18
  • Is there a URL we can test this at? Commented Apr 14, 2014 at 11:31
  • Afraid not - it's got to be quite secure due to the sensitivity of it and the information we're hosting. Sorry! I've just tried the following options (enableHighAccuracy: false, timeout: 120000, maximumAge: 60000) and it's still not working, but is refreshing quicker. Commented Apr 14, 2014 at 11:39
  • I've simplified the code but it's still not working. I think I'm finding WHERE the problem is - seemingly it's not actually managing to call the ShowPosition - it gets to the question but stops there. Commented Apr 14, 2014 at 13:51

1 Answer 1

1

The issue with his code is that the button was acting as a form submit action. So even though the browser paused to prompt for the OK, as soon as it was out of the way a form submission was fired. Adding return false to the end of handler should correct it (basically saying, "Dont submit this form"). I don't know ASP.Net very well so that may not be it exactly.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorted, thank you very much. The code is the same as above, except for as you pointed out, the button, which is now <asp:Button ID="btnLocate" Text="Loc" OnClientClick="GetLocation(); return false;" runat="server" /> (note the return false in the function call). Thanks a lot, this is absolutely perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.