Reputation: 623
i'm creating simple application to get current device's location when a button pressed. here's my code :
var win = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff',
layout:'vertical'
});
var btnGetLocation = Titanium.UI.createButton({
title:'Get Location (Once)',
width:'100dp'
});
btnGetLocation.addEventListener('click', function(e){
getDeviceLocation();
});
function getDeviceLocation(){
var longitude = 0.0;
var latitude = 0.0;
var altitude = 0;
var heading = 0;
var accuracy = 0;
var speed = 0;
var timestamp = '';
var altitudeAccuracy = 0;
var address = '';
var errMessage = '';
if (Titanium.Geolocation.locationServicesEnabled === false){
errMessage = 'Geolocation access turned off';
}
else{
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.distanceFilter = 10;
Titanium.Geolocation.getCurrentPosition(function(e){
if (!e.success || e.error){
errMessage = 'error: ' + JSON.stringify(e.error);
return;
}
if (typeof e.coords.longitude !== 'undefined' && e.coords.longitude !== null){longitude = e.coords.longitude;}
if (typeof e.coords.latitude !== 'undefined' && e.coords.latitude !== null){latitude = e.coords.latitude;}
if (typeof e.coords.altitude !== 'undefined' && e.coords.altitude !== null){altitude = e.coords.altitude;}
if (typeof e.coords.heading !== 'undefined' && e.coords.heading !== null){heading = e.coords.heading;}
if (typeof e.coords.accuracy !== 'undefined' && e.coords.accuracy !== null){accuracy = e.coords.accuracy;}
if (typeof e.coords.speed !== 'undefined' && e.coords.speed !== null){speed = e.coords.speed;}
if (typeof e.coords.timestamp !== 'undefined' && e.coords.timestamp !== null){timestamp = e.coords.timestamp;}
if (typeof e.coords.altitudeAccuracy !== 'undefined' && e.coords.altitudeAccuracy !== null){altitudeAccuracy = e.coords.altitudeAccuracy;}
});
// reverse geocoding
Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(e){
if (e.success) {
var places = e.places;
if (places && places.length) {
address = places[0].address;
} else {
address = "No address found";
}
}
else {
address = e.error;
}
alert('Longitude = ' + longitude + '\nLatitude = ' + latitude + '\nAltitude = ' + altitude + '\nHeading = ' + heading + '\nAccuracy = ' + accuracy + '\nSpeed = ' + speed + '\nTimestamp = ' + new Date(timestamp) + '\nAltitude Accuracy = ' + altitudeAccuracy + '\nAddress = ' + address + '\nError Message = ' + errMessage);
});
}
}
win.add(btnGetLocation);
win.open();
The condition is : device don't have any internet connection (data connection) and only GPS is turned on. when i press the button (get location in one shot), i can't get a new location.. the device still show my old location with old timestamp too and never get a new location even my current location is changed.. does anyone know how to get current location (one shot) only with GPS connection? many thanks..
Upvotes: 0
Views: 1727
Reputation: 806
If you use getCurrentPosition under Android this is not a Titanium bug. If you read the documentation:
*Retrieves the last known location from the device.
On Android, the radios are not turned on to update the location, and a cached location is used.
On iOS the radios may be used if the location is too "old".*
That means that getCurrentPosition (under Android) return the last position that has been cached by the system until an application (that uses location services) requires a new location (like google maps). So... what you can do is not to use getCurrentPosition when the application starts... but use an event called "location" that fires everytime a new location si available.
Luca
Upvotes: 2