【问题标题】:How to get current location in android 7?如何在android 7中获取当前位置?
【发布时间】:2017-09-03 10:56:54
【问题描述】:

我正在尝试获取 android 中的当前位置,并且当我在 android M 或更低版本上使用时它可以,但是当我在 android N 上运行我的应用程序时,位置为空,当我想从中获取纬度时会返回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();
        }

【问题讨论】:

    标签: android android-7.0-nougat locationlistener


    【解决方案1】:

    当我在 android M 或更低版本上使用时没问题

    不,不是。 getLastKnownLocation() 在所有 Android 版本上经常返回 null

    getLastKnownLocation() 是一种优化。使用它,但要准备好在 getLastKnownLocation() 返回 null 或过时值时依赖 onLocationChanged()

    这在the documentation 以及有关 Android 应用开发的书籍和课程中都有介绍。

    【讨论】:

    • 它在 android L 上对我有用,onLocationChangeListener 对我也不适用于 android 7 @CommonsWare
    • @BehnamEskandari: This sample app 在运行 Android 7.1 的 Google Pixel 上成功获取位置。对于美国的位置,它将显示该位置的天气预报。
    • 关于getLastKnownLocation()需要了解的主要一点是,如果您最近在设备上启用了定位,或者重新启动了设备,这将始终返回 null,直到在调用时收到定位来自任何应用程序的提供商(GPS、网络),而不仅仅是您的应用程序。仅仅因为你打电话给requestLocationUpdates() 并不意味着你马上就有了位置。此外,即使您确实从getLastKnownLocation() 获得了位置,也并不意味着它是最近的。它可能是几分钟或几小时。
    【解决方案2】:

    要获取最后一个已知位置,您只需拨打电话

    mLocationClient.getLastLocation(); 一旦连接了定位服务。

    了解如何使用新的位置 API

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2021-02-05
      • 2017-04-18
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多