2016-06-18 47 views
2

我想獲得用戶的位置,但該方法總是返回空值,這是我的代碼:獲取最後已知的位置總是返回NULL SDK 23

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    SystemProvider = locationManager.getBestProvider(new Criteria(),true); 
    UserLocation = locationManager.getLastKnownLocation(SystemProvider); 
      if(UserLocation==null) 
      { 
       Log.i("Exc2","No Location"); 
       mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("No Location")); 
       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(0,0),10)); 
      } 
      else { 
       double one = UserLocation.getLatitude(); 
       double two = UserLocation.getLongitude(); 
       mMap.addMarker(new MarkerOptions().position(new LatLng(one, two)).title("HMS")); 
       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(one, two), 10)); 
      } 

我已經嘗試過很多辦法,但是當我打開谷歌什麼都沒有發生,甚至地圖上的仿真器和壓在GPS按鈕,它是無法得到我的當前位置 由於事先

+0

查看一步一步的指導答案:http://stackoverflow.com/a/38397092/5955362 –

回答

0

檢查許可清單文件 android.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_FINE_LOCATION

嘗試手動傳遞服務位置

位置UserLocation = locationManager.getLastKnownLocation(「gps」);

如果你的Andorid 6.0,您必須在使用前位置檢查許可東陽它們可以被撤銷