0

i have page with button, when clicked shows an alert with latitude and longitude. It works on Chrome on my laptop but when i tried with my android mobile, the page is showing but the alert is not. I have tried changing watchPosition with getCurrentPosition, but no change.
In an attempt to solve it, i have put up an alert checkpoint and i have found out that the function success() is getting called in laptop browser but not in android browser, not even the function error() is being called. please help.

<html>
<head><title>Geolocation</title></head>
<body onload="getLocation();">
<input type="button" id="btnSearch" value="Search" onclick="btn_Click();"/>

<script>
    var pos;
    function btn_Click() {
        alert("Alert");
        //alert('Lat: ' + pos.coords.latitude + ' ' + 'Lon: ' + pos.coords.longitude);
    }

    function getLocation() {
        alert("doSomething");
        if (navigator.geolocation) {
            alert("navigator.geolocation");
            navigator.geolocation.getCurrentPosition(success,error,options);
            function success(position) {
                alert("success");
                pos = position;
            }
            function error(err) {
                alert("Error");
            };
            var options = {
                enableHighAccuracy: true,
                timeout: 5000,
                maximumAge: 0
            };
        } else {
            alert("I'm sorry, but geolocation services are not supported by your browser.");
        }

    }
</script>
</body>
</html>

i have even tried google gears, Now the gps sign is coming up on android but the location is still not showing....

<html>
<head><title>Javascript geo sample</title>
<script src="geo-min.js" type="text/javascript" charset="utf-8"></script>
</head> 
<body>
<b>Javascript geo sample</b>
<script>
    var pos;
    if (geo_position_js.init()) {
        geo_position_js.watchPosition(success_callback, error_callback, { enableHighAccuracy: true });
    }
    else {
        alert("Functionality not available");
    }

    function success_callback(p) {
        pos = p;
        alert('lat=' + p.coords.latitude.toFixed(2) + ';lon=' + p.coords.longitude.toFixed(2));
    }

    function error_callback(p) {
        alert('error=' + p.message);
    }

    function ChangeLabel() {
        document.getElementById("Label1").innerHTML =pos.coords.latitude;
    }
</script>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<input type="button" ID="Button1" value="Button" onclick="ChangeLabel()"/>
</body>
</html>
j4rey
  • 2,582
  • 20
  • 34
  • I think this is a problem with `alert()` support in android browsers. To be sure, instead of alert try to update some fields on the page with geo coordinates and see if it works. – Predrag Maric Oct 10 '14 at 11:06
  • the other alerts are working fine in android browsers.....but i still tried it, put a label and instead of alert i tried to update that on success()...it didnt work on android browser, but it worked on web browser – j4rey Oct 10 '14 at 11:15
  • do i need to add some script to use navigator.geolocation.getCurrentPosition();? to support android browser – j4rey Oct 10 '14 at 11:15
  • Maybe [this](http://stackoverflow.com/a/3368142/4074715) will help – Predrag Maric Oct 10 '14 at 11:35

1 Answers1

-2

Your code seems to be working perfectly fine.

For the Geo-location to work on an Android OS, you need to give a few permissions in the AndroidManifest.xml file, namely:

Uses Permissions:-

ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION

If you want the Geo-location functionality to work on all Android version, you also need to add:

WRITE_EXTERNAL_STORAGE
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80