简体   繁体   English

在android 4.0中定位到android studio中的位置问题

[英]problem in getting location in android 4.0 to up in android studio

i want to get user latitude and longtude in Service , i tried in android under 4.4 and all the thins work good but in android 4.4 to up i cant get user location already i have all permissions and location is enable . 我想在Service中获得用户的宽容度和长期服务,我在4.4以下的android中进行了尝试,并且所有功能都很好,但是在android 4.4中,我无法获取用户位置,我已经拥有所有权限并且启用了位置。

at the end : if anyway is for get location in service , without opening map please tell me 最后:如果仍要在服务中找到位置,请不要打开地图,请告诉我

this is my code: 这是我的代码:

private static final int REQUEST_LOCATION = 1;
LocationManager locationManager;
 double lattitude, longitude;





     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            buildAlertMessageNoGps();

        } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {




            if (ActivityCompat.checkSelfPermission(MyService.this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
                    (MyService.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions((Activity)getApplicationContext(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

            } else {
                Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);

                if (location != null) {
                    double latti = location.getLatitude();
                    double longi = location.getLongitude();
                    lattitude = latti;
                    longitude =longi;



                } else  if (location1 != null) {
                    double latti = location1.getLatitude();
                    double longi = location1.getLongitude();
                    lattitude = latti;
                    longitude =longi;



                } else  if (location2 != null) {
                    double latti = location2.getLatitude();
                    double longi = location2.getLongitude();
                    lattitude = latti;
                    longitude =longi;


                }else{

                    Log.d("Location", "Cant Get Location ");

                }
            }








        //}
    }

You are trying to get last known location. 您正在尝试获取最后的已知位置。 But are you sure that device has last known location? 但是,您确定该设备的最后已知位置吗? I think you have to request location. 我认为您必须请求位置。

Last known location is the location that device last obtained latitude and longitude. 最后已知位置是设备最后获得经度和纬度的位置。

For testing before calling your app first open internet goto map and then off the internet and open your app then you will get lastKnown location 在调用您的应用程序之前进行测试,请先打开Internet转到地图,然后断开Internet并打开您的应用程序,然后您将获得lastKnown位置

So last know location is depend on previous location. 所以最后知道的位置取决于先前的位置。 If the device has no previous location you will not get any lastKnown location. 如果设备没有先前的位置,则不会获得任何lastKnown位置。 So the location will be null. 因此location将为空。

So you have to request location. 因此,您必须请求位置。 locationmanager.requestLocationUpdates() for more details you can visit here. locationmanager.requestLocationUpdates()以获得更多详细信息,您可以在这里访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM