2012-05-14 26 views
14

Tôi đang làm việc trên ứng dụng Android 3.2+ trong Titanium. Điều quan trọng là tôi có thể xác định xem thiết bị có bật GPS hay không. Theo tham khảo API Titanium, Ti.Geolocation.locationServicesEnabled sẽ luôn trả về true trên Android 2.2+, do nhà cung cấp vị trí "thụ động" mới. Có cách nào khác để xác định xem GPS có thực sự được bật không?Làm cách nào để biết liệu GPS có được bật trên Android bằng Titanium

Cảm ơn.

+0

Kiểm tra bên dưới liên kết và thử nó .... http: //developer.appcelerator.com/question/120540/titaniumgeolocationlocationservicesenabled-is-always-true – JohnWhite

Trả lời

1

tôi nghĩ rằng mã này sẽ làm việc cho bạn:

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

refrence

+1

có lỗi runtime không có phương pháp isLocationProviderEnabled thế nào tôi có thể sửa lỗi này –

0

Được rồi đây là một giải pháp dễ dàng tôi đã đưa ra và nó hoạt động tốt đối với tôi. Tôi có một biến toàn cầu 'timeStamp' mà ban đầu tôi đặt thành 0.

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; 


     } 
    });