获取上一个已知位置始终返回NULL SDK 23

时间:2016-06-18 00:34:34

标签: android google-location-services

我想获取用户位置,但该方法始终返回null这是我的代码:

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

我尝试了很多方法,但即使我在模拟器上打开Goog​​le地图并按下gps按钮也无法获取当前位置 提前谢谢

2 个答案:

答案 0 :(得分:0)

检查清单文件中的权限 android.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_FINE_LOCATION

尝试手动传递服务位置

位置UserLocation = locationManager.getLastKnownLocation(" gps");

如果您的andorid是6.0,您必须在使用位置之前检查是否允许,因为它们可以被撤销

答案 1 :(得分:0)

尝试使用融合位置API 接收来自Google的位置更新。 检查此网址:https://developer.android.com/training/location/receive-location-updates.html它会对您有所帮助。

相关问题