How to get initial location with LocationManager?

ぐ巨炮叔叔 提交于 2019-11-28 08:53:54

问题


I've been getting current location with GoogleApiClient up until now but I've just noticed that it's much simpler to do it with LocationManager using LocationListener since it can even detect when GPS service was turned on or off by the user.

But I have a problem when getting user's first location after the LocationManager was initialized.

LocationManager has 4 listeners but none of them give you your first location. It does have a onLocationChanged listener but it only activates when you move.

This is how I'm using it:

// 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) {
}

回答1:


Use the following method to get the Location object:

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

For more information on the method getLastKnownLocation, please refer to the docs.



来源:https://stackoverflow.com/questions/30237729/how-to-get-initial-location-with-locationmanager

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