All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.osmdroid.util.LocationUtils Maven / Gradle / Ivy

There is a newer version: 6.1.20
Show newest version
package org.osmdroid.util;

import org.osmdroid.util.constants.UtilConstants;

import android.location.Location;
import android.location.LocationManager;

public class LocationUtils implements UtilConstants {

	/**
	 * This is a utility class with only static members.
	 */
	private LocationUtils() {
	}

	/**
	 * Get the most recent location from the GPS or Network provider.
	 * @param pLocationManager
	 * @return return the most recent location, or null if there's no known location
	 */
	public static Location getLastKnownLocation(final LocationManager pLocationManager) {
		if (pLocationManager == null) {
			return null;
		}
		final Location gpsLocation =
			pLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		final Location networkLocation =
			pLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (gpsLocation == null) {
			return networkLocation;
		} else if (networkLocation == null) {
			return gpsLocation;
		} else {
			// both are non-null - use the most recent
			if (networkLocation.getTime() > gpsLocation.getTime() + GPS_WAIT_TIME) {
				return networkLocation;
			} else {
				return gpsLocation;
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy