简体   繁体   English

如何在Android 7中获取当前位置?

[英]How to get current location in android 7?

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 我试图在android M或更低版本上使用时在android中获取当前位置,并且确定,但是当我在android N上运行我的应用程序时,位置为null,并且当我想从中获取纬度时将返回0。是我的代码

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

and its OK when i'm using on android M or lower 和我在android M或更低版本上使用时的确定

No, it is not. 不它不是。 getLastKnownLocation() frequently returns null , on all versions of Android. 在所有版本的Android上, getLastKnownLocation()经常返回null

getLastKnownLocation() is an optimization. getLastKnownLocation()是一种优化。 Use it, but be prepared to rely upon onLocationChanged() for when getLastKnownLocation() returns null or an out-of-date value. 使用它,但是准备onLocationChanged() getLastKnownLocation()返回null或过期值时依赖于onLocationChanged()

This is covered in the documentation , as well as in books and courses on Android app development. 文档 ,以及有关Android应用程序开发的书籍和课程都对此进行了介绍

To get the last known location all your have to do is call 要获得最后的已知位置,您只需致电

mLocationClient.getLastLocation(); mLocationClient.getLastLocation(); once the location service was connected. 连接定位服务后。

read how to use the new location API 阅读如何使用新的位置API

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

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

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