2012-03-07 97 views
0

在我的應用程序中,我想獲取用戶的位置並在地圖上顯示它(谷歌地圖)。 在Android設備的設置,您可以檢查Android谷歌地圖使用GPS位置不是網絡

  1. 使用無線網絡
  2. 使用GPS衛星

當用戶開始我顯示至極一個對話框,用戶可以選擇獲得mapactivity他現在的位置與否。

Old code deleted 

如果用戶想要得到他的位置,我檢查是否啓用了gps。如果沒有,我開始設置。否則,我嘗試獲取位置。但即使GPS處於活動狀態並且「使用無線網絡」,他也不會獲得位置信息。是否有解決方案來檢查是否啓用了「使用無線網絡」。或者我需要更改哪部分代碼才能通過GPS信號獲取位置?

感謝

編輯mycurrentcode:

boolean isGps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
       boolean isNetwork = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

       if(!isGps && !isNetwork){ 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent); 
       } else if(isGps && !isNetwork) { 
        String bestProvider = LocationManager.GPS_PROVIDER; 
        Location location = lm.getLastKnownLocation(bestProvider); 


        Toast.makeText(MapsTabActivitiy.this, "Location niet beschikbaar.", Toast.LENGTH_SHORT).show(); 
       } else if(!isGps && isNetwork) { 
        String bestProvider = LocationManager.NETWORK_PROVIDER; 
        Location location = lm.getLastKnownLocation(bestProvider); 

        if(location == null){ 
         location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        } else if (location != null){ 
         mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6))); 
         mapcontroller.setZoom(15); 
        } 
       } 
       else { 
        Criteria criteria = new Criteria(); 
        criteria.setAccuracy(Criteria.ACCURACY_FINE); 
        String bestProvider = lm.getBestProvider(criteria, false); 
        Location location = lm.getLastKnownLocation(bestProvider); 

        if(location == null){ 
         location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        } else if (location != null){ 
         mapcontroller.animateTo(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6))); 
         mapcontroller.setZoom(15); 
        } 
       } 

所以我isProviderEnabled檢查GPS_PROVIDER和NETWORK_PROVIDER。並與如果,否則,如果我改變如何獲得位置。我需要找到唯一的情況是案例2(否則,如果(是GPS & &!isNetwork){})。通過GPS信號獲取位置。

回答

0

您正在使用Criteria類獲得合適的提供商,但您可以使用LocationManager.GPS_PROVIDER簡單地請求僅使用GPS的位置更新。更改

String bestProvider = lm.getBestProvider(criteria, false); 

String bestProvider = LocationManager.GPS_PROVIDER; 

希望這有助於。

+0

現在,即使當「使用無線網絡」時,他也沒有找到我的位置 – 2012-03-07 09:44:38

+0

@TimonDevos,並且「使用gps衛星」是否被檢查? – Egor 2012-03-07 09:49:44

+0

但我希望設備在運行時選擇至極位置更準確。如果我使用LocationManager.GPS_PROVIDER,那麼設備不會選擇?只有當NETWORK_PROVIDER未啓用時,他需要從GPS_PROVIDER獲取位置,反之亦然 – 2012-03-07 09:55:26