ridan
ridan

Reputation: 868

Phonegap Geolocalisation application crashes

I'm trying to get current localisation using phonegap javascript api. Here is a sample code taken from phonegap website:

<script type="text/javascript" charset="utf-8">

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

// PhoneGap is ready
//
function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

// onSuccess Geolocation
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                        'Longitude: '          + position.coords.longitude             + '<br />' +
                        'Altitude: '           + position.coords.altitude              + '<br />' +
                        'Accuracy: '           + position.coords.accuracy              + '<br />' +
                        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                        'Heading: '            + position.coords.heading               + '<br />' +
                        'Speed: '              + position.coords.speed                 + '<br />' +
                        'Timestamp: '          + new Date(position.timestamp)          + '<br />';
}

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

</script>

If I remove geolocalisation code, the application works well.

Any ideas ?

Thanks in advance,

Upvotes: 0

Views: 805

Answers (3)

Neji
Neji

Reputation: 6839

if you are trying it on android device then altitudeAccuracy property is not support by Android devices, it will always return null.

Upvotes: 0

DavidB
DavidB

Reputation: 2234

If you are seeing the error in Android, you want to add the enableHighAccuracy flag:

navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: true });

Upvotes: 1

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

I have just tried it on PhoneGap 1.4.1, iOS Simulator 5.0 and it works fine.

What kind of error are you getting ?

Upvotes: 0

Related Questions