Reputation: 28
I'm newbie with phonegap. I'm creating an app which gets the location of the device...I have tested with samsung galaxy tab and htc, but doesn't do anything...I try to get the speed of the device, and display it in a div in screen. This is my code:
GEOLOCATION:
// Phonegap loads
document.addEventListener("deviceready", onDeviceReady, false);
var watchIDgeolocation = null;
var watchIDaccelerometer = null;
// PhoneGap has loaded
function onDeviceReady() {
var options = { frequency: 1000 }; // I want obtain data each 1 s
watchIDgeolocation = navigator.geolocation.watchPosition(onSuccessGeolocation, onErrorGeolocation, options);
startWatch();
}
// Success
var onSuccessGeolocation = function(position) {
// These are functions that displays data in screen
cambioMarchas(position.coords.speed);
excesoVelocidad(position.coords.speed);
paradaProlongada(position.coords.speed);
aceleracionBrusca(position.coords.speed);
deceleracionBrusca(position.coords.speed);
accidenteDeTrafico(position.coords.speed);
// ERROR
function onErrorGeolocation(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
I have tested in Ripple, the extension of Chrome, and it works fine, changing values of the speed, and it works fine...but with device, I don't know why not. Why can be? Regards, Daniel
I have read that maybe is needed to add { enableHighAccuracy: true } in var options...but I don't understand this...
Upvotes: 0
Views: 388
Reputation: 66
Well Daniel, first I recommend you to try this common PhoneGap app to test if the device ready for use or not, if so, it'll alert you with a display alert message, meanwhile, if it's working perfect, so you got a problem with your code and so go back n check it. Here you are:
// Global variable that will tell us whether PhoneGap is ready
var isPhoneGapReady = false;
function init() {
// Add an event listener for deviceready
document.addEventListener("deviceready",
onDeviceReady, false);
// Older versions of Blackberry < 5.0 don't support
// PhoneGap's custom events, so instead we need to perform
// an interval check every 500 milliseconds to see whether
// PhoneGap is ready. Once done, the interval will be
// cleared and normal processing can begin.
intervalID = window.setInterval(function() {
if (PhoneGap.available) {
onDeviceReady();
}
}, 500);
}
function onDeviceReady() {
window.clearInterval(intervalID);
// set to true
isPhoneGapReady = true;
alert('The device is now ready');
}
// Set an onload handler to call the init function
window.onload = init;
Upvotes: 3