【问题标题】:How to get initial location with LocationManager?如何使用 LocationManager 获取初始位置?
【发布时间】:2015-05-14 12:44:33
【问题描述】:

到目前为止,我一直在使用GoogleApiClient 获取当前位置,但我刚刚注意到使用LocationManager 使用LocationListener 执行此操作要简单得多,因为它甚至可以检测 GPS 服务何时打开或由用户关闭。

但是在初始化LocationManager 后获取用户的第一个位置时出现问题。

LocationManager 有 4 个听众,但他们都没有给你你的第一个位置。它确实有一个onLocationChanged 监听器,但它仅在您移动时才会激活。

这就是我的使用方式:

// Init LocationManager (needed to track if GPS is turned on or not
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


end of oncreate......


/*
LocationListener (Listening if GPS service is turned on/off)
 */

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderDisabled(String provider) {
}

【问题讨论】:

    标签: android locationmanager


    【解决方案1】:

    使用以下方法获取Location对象:

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    
            // getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            Log.v(TAG, "isGPSEnabled =" + isGPSEnabled);
    
            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            Log.v(TAG, "isNetworkEnabled =" + isNetworkEnabled);
    
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d(TAG, "Network");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled && location == null) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d(TAG, "GPS Enabled");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Location Not Found");
        }
        return location;
    }
    

    更多方法getLastKnownLocation,请refer to the docs

    【讨论】:

    • 不,永远不要使用此代码。它有很多错误,包括它并不总是使用 GPS,可能返回过时的数据或 null,即使它说有,它也不总是有一个位置。请参阅gabesechansoftware.com/location-tracking 了解完整的文章
    • @GabeSechan,您的链接已关闭,您可以更新它或以某种方式发送代码吗?我很好奇为什么这段代码这么糟糕。
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 2016-05-21
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多