How to get current location in android 7?

浪尽此生 提交于 2020-01-11 11:30:13

问题


I'm trying to get current location in android and its OK when i'm using on android M or lower, But when i run my application on android N, location is null and when i want get latitude from it will returns 0. here is my code

 try {

            if (!isGPSEnabled && !isNetworkEnabled) {
             } else {
                this.canGetLocation = true;
                 if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                 if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

回答1:


and its OK when i'm using on android M or lower

No, it is not. getLastKnownLocation() frequently returns null, on all versions of Android.

getLastKnownLocation() is an optimization. Use it, but be prepared to rely upon onLocationChanged() for when getLastKnownLocation() returns null or an out-of-date value.

This is covered in the documentation, as well as in books and courses on Android app development.




回答2:


To get the last known location all your have to do is call

mLocationClient.getLastLocation(); once the location service was connected.

read how to use the new location API

http://developer.android.com/training/location/retrieve-current.html#GetLocation



来源:https://stackoverflow.com/questions/46022725/how-to-get-current-location-in-android-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!