3

I'm doing a small sample app which displays latitude and longitude in a popup when i click on the button here is my code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>BlankCordovaApp1</title>

    <link href="css/index.css" rel="stylesheet" />

    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/index.js"></script>

    <script type="text/javascript" charset="utf-8">
        var alertmsg = function (position) {
            var msg = 'Latitude: ' + position.coords.latitude + '<br />' +
                'Longitude: ' + position.coords.longitude + '<br />'
            navigator.notification.alert(msg);
        }
        function geoLocation() {
            navigator.geolocation.getCurrentPosition(alertmsg)
        }
    </script>
</head>
<body>
    <input type="button" id="btnClick" onclick="geoLocation()" value="click" />
</body>
</html>

It is working in Ripple emulator But it is not working in Android emulator and Genymotion

mahindar
  • 705
  • 10
  • 23
  • 1
    in android emulator you can't get geolocations use real devices. – Aravin Nov 05 '14 at 11:57
  • There is way by telenet to fix geo in android emulator but that too didn't worked for me.what about the genymotion? can we get the geolocations in it? Also can we install bin\android\debug\App2-debug-unaligned.apk directly to the device?(I can't test it b'coz i don't have a android device now) – mahindar Nov 05 '14 at 13:49

2 Answers2

1

I figured out the problem. If i use this code it is working fine

navigator.geolocation.getCurrentPosition(alertmsg, onError, { timeout: 30000, enableHighAccuracy: true });

It is working in all emulators(Ripple,Android,Genymotion)

mahindar
  • 705
  • 10
  • 23
1

I'm using Visual Studio 13 with a Backbone based smartphone app and this was a pain. Adding the timeout option and enableHighAccuracy always throws the onError handler, without these neither return.

So this is a good answer:

//Android Emulator safe version
function getGpsCordinates(callback) {
    if ("geolocation" in navigator) {                  
        navigator.geolocation.getCurrentPosition(
            //Success
            function (position) {
                console.log("GPS: Success");
                callback(position);
            },
            //Error
            function (error) {
                console.log("GPS: Error");
                var position = {
                  coords: {
                      longitude: 0,
                      latitude: 0,
                      speed: 0
                  }
              };
              callback(position);
            },
            { timeout: 7000, enableHighAccuracy: true });
    } else {
        var position = {
            coords: {
                longitude: 0,
                latitude: 0,
                speed: 0
            }
        };
        console.log("GPS: Not Supported");
        callback(position);
    }
    console.log("GPS: Continued");
}

getGpsCordinates(function(mycallback) {
  alert(mycallback.coords.latitude);
});

Code Pen Version

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
MagicLuckyCat
  • 324
  • 2
  • 5
  • 19