14

我正在 Titanium 中开发 Android 3.2+ 应用程序。我能够确定设备是否启用了 GPS 至关重要。根据 Titanium API 参考,Ti.Geolocation.locationServicesEnabled 在 Android 2.2+ 上将始终返回 true,因为有一个新的“被动”位置提供程序。有没有其他方法可以确定 GPS 是否真正启用?

谢谢。

4

2 回答 2

1

我认为这段代码应该适合你:

//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();
}

参考

于 2013-04-03T12:03:05.313 回答
0

好吧,这是我想出的一个简单的解决方案,它对我来说效果很好。我有一个全局变量“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;


        }
    });     
于 2013-10-16T17:47:48.810 回答