我正在创建简单的应用程序以在按下按钮时获取当前设备的位置。这是我的代码:
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();
条件是:设备没有任何互联网连接(数据连接),只有 GPS 已打开。当我按下按钮(一次获得位置)时,我无法获得新位置..设备仍然显示我的旧位置和旧时间戳,即使我当前的位置改变了也永远不会获得新位置..有人吗知道如何仅通过 GPS 连接获取当前位置(一次拍摄)吗?非常感谢..