簡體   English   中英

Android:GPS位置無法正常工作

[英]Android: GPS Location not working

我正在嘗試按以下方式檢索位置:

LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);        

Criteria criteria = new Criteria();

// Getting the name of the provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, false);

if(provider!=null && !provider.equals("")){
   // NOTE: the location below is null !!
   Location location = locationManager.getLastKnownLocation(provider);
   String lat = location.getLatitude();
   String lng = location.getLongitude();
}

上面的“位置”為空。 為什么?

但是,當我打開(Google)地圖應用程序時-它正確顯示了位置,甚至顯示了一個通知圖標(看起來像一個感嘆號)。

並且有一個GPS坐標應用程序-也顯示空白。

我已在手機上打開“設置”>“位置”。 如果有問題,請使用Android 4.4.4 Sony Experia手機。

好吧,看來您沒有正確檢查GPS是否在工作。 您應該采取的方式:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { 
    //GPS is not enabled !! 
} 

您還可以創建一個AlertDialog,以使用戶意識到未啟用GPS,可以通過以下操作將其發送給GPS設置:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

之后,我將實現一個位置偵聽器並獲取坐標,這是一個示例,請檢查一下

如何在Android中以編程方式獲取當前GPS位置?

更新:他們可能正在通過其他提供者(通常是那時候工作得更好的提供者)獲取坐標。 因此,如果GPS無法正常工作,請嘗試其他提供商,這是一個示例:

 Location lastKnownLocation = null;
 List<String> providers = null;
 if(locationManager != null) providers = locationManager.getAllProviders();

        if(providers != null)
        {
            for(int i=0; i<providers.size(); i++)
            {
                if(locationManager != null) lastKnownLocation = locationManager.getLastKnownLocation(providers.get(i));
                if(lastKnownLocation != null)
                {
                    position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                    break;
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM