2

我想获取用户位置,但该方法总是返回 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));
           }

我尝试了很多方法,但即使我在模拟器上打开谷歌地图并按下 gps 按钮,它也无法获得我当前的位置,但在此先感谢

4

2 回答 2

0

尝试使用Fused Location API在 Google 推荐的位置接收位置更新。检查这个网址:https ://developer.android.com/training/location/receive-location-updates.html它会帮助你。

于 2016-06-18T03:27:57.310 回答
0

check permission in Manifest file android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION

try passing service location manually

Location UserLocation = locationManager.getLastKnownLocation("gps");

if your andorid is 6.0 you must check for permission before use location beacause they can be revoked

于 2016-06-18T01:00:43.257 回答