我正在使用Titanium中的Android 3.2+應用程序。我能夠確定設備是否啓用了GPS是至關重要的。根據Titanium API參考,Ti.Geolocation.locationServicesEnabled將始終在Android 2.2+上返回true,因爲有一個新的「被動」位置提供程序。有沒有其他方法可以確定GPS是否真正啓用?如何判斷GPS是否在Android上使用Titanium啓用
謝謝。
我正在使用Titanium中的Android 3.2+應用程序。我能夠確定設備是否啓用了GPS是至關重要的。根據Titanium API參考,Ti.Geolocation.locationServicesEnabled將始終在Android 2.2+上返回true,因爲有一個新的「被動」位置提供程序。有沒有其他方法可以確定GPS是否真正啓用?如何判斷GPS是否在Android上使用Titanium啓用
謝謝。
我認爲這代碼應該爲你工作:
//check to see if we have GPS capabilities
if(Titanium.Geolocation.isLocationProviderEnabled(Titanium.Geolocation.PROVIDER_GPS, Titanium.Geolocation.ACCURACY_BEST) == false) {
var alertDlg = Titanium.UI.createAlertDialog({
title:'MileTrackGPS',
message:'GPS is OFF. Enable it in Settings.',
buttonNames: ['Cancel', 'Open Settings']
});
alertDlg.cancel = 0;
alertDlg.addEventListener('click', function(e){
if(!e.cancel) {
//open up the settings page
var settingsIntent = Titanium.Android.createIntent({
action: 'android.settings.LOCATION_SOURCE_SETTINGS'
});
activity.startActivity(settingsIntent);
}
else {
//close the window to exit
win.close();
}
});
alertDlg.show();
}
有運行時錯誤
好吧,這裏是一個簡單的解決方案,我想出了,它很適合我。我有一個全局變量'timeStamp',我最初設置爲零。
Titanium.Geolocation.getCurrentPosition(function(e){
//only update fields if timer is still active
if(gpsTimer!=null)
{
//if the provider is not GPS or the timestamp is the same as the last, we do not want the results. We need to alert the user that they need to turn their GPS on.
if(e.provider['name']!="gps" || timeStamp==e.coords.timestamp)
{
//clear timeout
clearTimeout(gpsTimer);
gpsTimer = null;
//close window
get_gps_win.close();
//garbage collection
get_gps_win = null;
gpsLatField = null;
gpsLongField = null;
gpsAccuracyField = null;
timeStamp=0;
//alert user
alert("This feature is not available unless you have GPS turned on. Please turn GPS on and then try again.");
}
else
{
//update fields
gpsLatField.value=ConvertDDToDMSPlain(e.coords.latitude);
gpsLongField.value=ConvertDDToDMSPlain(e.coords.longitude);
gpsAccuracyField.value=e.coords.accuracy+" meters/"+(e.coords.accuracy*3.28084)+" feet";
gpsTimer=setTimeout(function() {
Titanium.Geolocation.fireEvent('location');
}, 1000);
}
timeStamp= e.coords.timestamp;
}
});
檢查下面的鏈接和嘗試.... HTTP://developer.appcelerator.com/question/120540/titaniumgeolocationlocationservicesenabled-is-always-true – JohnWhite