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?