简体   繁体   中英

Latitude Longitude tracking in Android Phonegap?

I am new to android platform so I wanted to start my learning with existing sample code. I am trying to acquire GPS Coordinates so i wote the below code. In the below code once the device is started onDeviceReady() method is firing but onSuccess() method is not firing. How to solve this issue? I am using cordova-2.8.0.jar in my project.

<!DOCTYPE html>
<html xml:lang="en" lang="en">
    <head>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>  
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;">
        <title>PhoneGap</title>
        <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    var watchID = null;

    // Cordova is ready
    //
    function onDeviceReady() {
        // Throw an error if no update is received every 30 seconds
        alert("Test1");
        var options = {
         timeout: 3000
          };
        alert("TEst2"+options);
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
        alert("TEst3");
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {
    alert("RAmesh");
       /* var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                            'Longitude: ' + position.coords.longitude     + '<br />' +
                            '<hr />'      + element.innerHTML;*/
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
      /*  alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');*/
    }

    </script>
  </head>
  <body>
    <p id="geolocation">Finding geolocation...</p>
  </body>
</html>

Try changing your options as follows:

var options = {
  enableHighAccuracy: true,
  timeout: 30000,
  maximumAge: 0
};

Ensure you have set permission to use location in AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

and in your config.xml:

<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />

First, try the instruction of Dpa99c.

Second, use GenyMotion Emulator. It's a awsome emulator for Android : faster than default and you have some widget to modify the location, camera and much more.

Third, I just discover one masive problem in Cordova/PhoneGap : - When you open the Android project built by cordova (in commandline), the requestUpdate is set at ..... 60 Seconds !!!!!!!!!!! You have a new Location every Minute ! Evenif you use GPS (enableHighAccuracy="true"). So try to set this at 1000 where you find LocationManager.GPS_PROVIDER in org.apache.cordova.geolocation package -> GPSListener class !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM