0

I've created a demo PhoneGap application, that checks acceleration, compass and geolocation and presents these info to user. Application was compiled using PhoneGap Build.

I used simple toFixed(n) and some strings to round value and add some unit after it. This worked just great in case of acceleration and compass. For some reason beyond my imaggination, this fails in case of geolocation (at least on Galaxy Nexus / Android 4.3).

I used such function:

function onGeolocationSuccess(position)
{
    console.log(position);

    var coords = position.coords;

    coords.latitude = coords.latitude.toFixed(6);
    coords.longitude = coords.longitude.toFixed(6);
    coords.altitude = coords.altitude.toFixed(2) + ' m';
    coords.accuracy = coords.accuracy.toFixed(6) + ' m';
    coords.altitudeAccuracy = coords.altitudeAccuracy.toFixed(2) + ' m';
    coords.heading = coords.heading.toFixed(2) + '\u00b0';
    coords.speed = coords.speed.toFixed(2) + ' m/s';

    var geoText = 'Longitude is ' + coords.longitude + '.';

    geoText = geoText + 'Latitude is ' + coords.latitude + '.';
    geoText = geoText + 'Accuracy is ' + coords.accuracy + '.';
    geoText = geoText + 'Altitude is ' + coords.altitude + '.';
    geoText = geoText + 'Altitude Accuracy is ' + coords.altitudeAccuracy + '.';
    geoText = geoText + 'Heading is ' + coords.heading + '.';
    geoText = geoText + 'Speed is ' + coords.speed + '.';

    $('#positionText').html(geoText);
}

It is passed as success callback of PhoneGap's navigator.geolocation.watchPosition.

Function fires, entire position object is written to console and that's all. If lines between var coords = position.coords; and var geoText = 'Longitude is ' + coords.longitude + '.'; are not commented, function execution dies somewhere in between, line $('#positionText').html(geoText); is not being executed and positionText div is not being updated correctly.

If I comment out these seven "rounding" lines all is fine and div is being updated. The same rounding works like a charm in case of acceleration and compass object, which makes this even more strange.

Can someone tell me, what am I missing? Why PhoneGap geolocation object doesn't like rounding, while compass and acceleration objects goes fine with this?

trejder
  • 17,148
  • 27
  • 124
  • 216
  • Is any part of your position object coming back null? I put your code in a jsfiddle [here](http://jsfiddle.net/andrewlively/LW4Uz/1/), played with it a little and got it to work just fine. It only broke when trying to do .toFixed() on a null value, it's not a problem with Phonegap – Andrew Lively Aug 09 '13 at 01:43
  • Seems, that you got the point (so obvious, though! :|). Please, reformulate this comment as full-size answer, so I can accept it and award your work, with some rep point. – trejder Aug 09 '13 at 08:45

1 Answers1

1

Part of your position object was coming back null, which was killing your code when the toFixed() was being applied to those null values. Here's how I got it working:

jsfiddle

HTML

<div id="positionText"></div>

Javascript

function onGeolocationSuccess(position) {

    var coords = {};

    coords.latitude = position.coords.latitude.toFixed(6);
    coords.longitude = position.coords.longitude.toFixed(6);
    coords.altitude = position.coords.altitude + ' m';
    coords.accuracy = position.coords.accuracy + ' m';
    coords.altitudeAccuracy = position.coords.altitudeAccuracy + ' m';
    coords.heading = position.coords.heading + '\u00b0';
    coords.speed = position.coords.speed + ' m/s';

    var geoText = 'Longitude is ' + coords.longitude + '.<br/>';

    geoText += 'Latitude is ' + coords.latitude + '.<br/>';
    geoText += 'Accuracy is ' + coords.accuracy + '.<br/>';
    geoText += 'Altitude is ' + coords.altitude + '.<br/>';
    geoText += 'Altitude Accuracy is ' + coords.altitudeAccuracy + '.<br/>';
    geoText += 'Heading is ' + coords.heading + '.<br/>';
    geoText += 'Speed is ' + coords.speed + '.<br/>';

    $('#positionText').html(geoText);
}

function onError() {

}

navigator.geolocation.getCurrentPosition(onGeolocationSuccess, onError);
Andrew Lively
  • 2,145
  • 2
  • 20
  • 27