user25730
user25730

Reputation: 709

HTML5 geolocation - not working on Android

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.

Upvotes: 0

Views: 2111

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

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.

Upvotes: 1

Related Questions