Add a marker and circle of accuracy for current location

Currently the user's position is not visible on the map. This
commit adds a marker to the user's position and a circular
polygon which shows the accuracy of the location measurement.
This commit is contained in:
Tobias Schönberg 2017-05-28 17:50:53 +02:00
parent 4b400cd8aa
commit 4f4a875ebe
6 changed files with 73 additions and 7 deletions

View file

@ -4,6 +4,7 @@ public class LatLng {
public final double latitude;
public final double longitude;
public final float accuracy;
/** Accepts latitude and longitude.
* North and South values are cut off at 90°
@ -11,13 +12,14 @@ public class LatLng {
* @param latitude double value
* @param longitude double value
*/
public LatLng(double latitude, double longitude) {
public LatLng(double latitude, double longitude, float accuracy) {
if(-180.0D <= longitude && longitude < 180.0D) {
this.longitude = longitude;
} else {
this.longitude = ((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D;
}
this.latitude = Math.max(-90.0D, Math.min(90.0D, latitude));
this.accuracy = accuracy;
}
public int hashCode() {

View file

@ -14,6 +14,7 @@ public class LocationServiceManager implements LocationListener {
private String provider;
private LocationManager locationManager;
private LatLng latestLocation;
private Float latestLocationAccuracy;
public LocationServiceManager(Context context) {
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
@ -24,6 +25,16 @@ public class LocationServiceManager implements LocationListener {
return latestLocation;
}
/**
* Returns the accuracy of the location. The measurement is
* given as a radius in meter of 68 % confidence.
*
* @return Float
*/
public Float getLatestLocationAccuracy() {
return latestLocationAccuracy;
}
/** Registers a LocationManager to listen for current location.
*/
public void registerLocationManager() {
@ -57,9 +68,10 @@ public class LocationServiceManager implements LocationListener {
public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
Timber.d("Latitude: %f Longitude: %f", currentLatitude, currentLongitude);
latestLocationAccuracy = location.getAccuracy();
Timber.d("Latitude: %f Longitude: %f Accuracy %f", currentLatitude, currentLongitude, latestLocationAccuracy);
latestLocation = new LatLng(currentLatitude, currentLongitude);
latestLocation = new LatLng(currentLatitude, currentLongitude, latestLocationAccuracy);
}
@Override