Adam
Adam

Reputation: 27

Geolocation using PhoneGap on Android works only on index.html

I'm trying to use geolocation using PhoneGap API doc, geolocation works on the index.html but when I try to use the same function in a different page it doesn't work.

http://docs.phonegap.com/en/1.8.1/cordova_geolocation_geolocation.md.html#Geolocation

This is the JS I'm using.

This is the button I'm using to get to the other page.

I'm also using jQuery Mobile.

 <a href="test.html" data-theme="b" data-icon="plus">Add category</a></li>

here is the head of test.html

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<link rel="stylesheet" href="styles/jquery.mobile-1.1.0.min.css" />
<link rel="stylesheet" href="styles/jquery.mobile.structure-1.1.0.min.css" />
<link rel="stylesheet" href="styles/jquery.mobile.theme-1.1.0.min.css" />
<link rel="stylesheet" href="styles/my.css" />
<script src="scripts/jquery-1.7.2.min.js"></script>
<script src="scripts/jquery.mobile-1.1.0.min.js"></script>
<script src="scripts/cordova-1.8.1.js"></script>
<script>

    // 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
        var options = { timeout: 10000 };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {
        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>

Upvotes: 1

Views: 3934

Answers (2)

user1754564
user1754564

Reputation: 36

First give permissions in Android Manifest:

      android.permission.ACCESS_FINE_LOCATION
      android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
      android.permission.ACCESS_COARSE_LOCATION

then copy your cordova-1.8.1.js and create index.html inside www subfolder of assets and after that copy cordova-1.8.1.jar to libs and then configure build path with cordova-1.8.1.jar.

then create res-->xml and copy plugins(from earlier downloaded PhoneGap 1.8.1).

then run the application. Definitely you will get the output.

Upvotes: 2

Adam
Adam

Reputation: 27

The problem was I didn't load the cordova.jar file to my project and that's why it didn't work

Upvotes: 0

Related Questions