LocationView: Store coordinate as a Point, rather than as a lat/lon pair.

This commit is contained in:
Andreas Schildbach 2018-11-16 19:46:44 +01:00
parent 696e7ddc19
commit 5f44a4e6db

View file

@ -81,7 +81,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
private LocationType locationType = LocationType.ANY; private LocationType locationType = LocationType.ANY;
private String id = null; private String id = null;
private double lat, lon; private Point coord;
private String place; private String place;
public LocationView(final Context context) { public LocationView(final Context context) {
@ -109,8 +109,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
state.putParcelable("super_state", super.onSaveInstanceState()); state.putParcelable("super_state", super.onSaveInstanceState());
state.putSerializable("location_type", locationType); state.putSerializable("location_type", locationType);
state.putString("location_id", id); state.putString("location_id", id);
state.putDouble("location_lat", lat); state.putSerializable("location_coord", coord);
state.putDouble("location_lon", lon);
state.putString("location_place", place); state.putString("location_place", place);
state.putString("text", getText()); state.putString("text", getText());
state.putString("hint", hint); state.putString("hint", hint);
@ -125,8 +124,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
super.onRestoreInstanceState(bundle.getParcelable("super_state")); super.onRestoreInstanceState(bundle.getParcelable("super_state"));
locationType = ((LocationType) bundle.getSerializable("location_type")); locationType = ((LocationType) bundle.getSerializable("location_type"));
id = bundle.getString("location_id"); id = bundle.getString("location_id");
lat = bundle.getDouble("location_lat"); coord = (Point) bundle.getSerializable("location_coord");
lon = bundle.getDouble("location_lon");
place = bundle.getString("location_place"); place = bundle.getString("location_place");
setText(bundle.getString("text")); setText(bundle.getString("text"));
hint = bundle.getString("hint"); hint = bundle.getString("hint");
@ -289,8 +287,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public void onLocationStart(final String provider) { public void onLocationStart(final String provider) {
locationType = LocationType.COORD; locationType = LocationType.COORD;
lat = 0; coord = null;
lon = 0;
hint = res.getString(R.string.acquire_location_start, provider); hint = res.getString(R.string.acquire_location_start, provider);
updateAppearance(); updateAppearance();
@ -317,8 +314,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public void reset() { public void reset() {
locationType = LocationType.ANY; locationType = LocationType.ANY;
id = null; id = null;
lat = 0; coord = null;
lon = 0;
place = null; place = null;
setText(null); setText(null);
hint = null; hint = null;
@ -331,18 +327,17 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public void setLocation(final Location location) { public void setLocation(final Location location) {
locationType = location.type; locationType = location.type;
id = location.id; id = location.id;
lat = location.getLatAsDouble(); coord = location.coord;
lon = location.getLonAsDouble();
place = location.place; place = location.place;
setText(location.uniqueShortName()); setText(location.uniqueShortName());
updateAppearance(); updateAppearance();
if (locationType == LocationType.COORD) { if (locationType == LocationType.COORD && coord != null) {
hint = res.getString(R.string.directions_location_view_coordinate) + ": " hint = res.getString(R.string.directions_location_view_coordinate) + ": "
+ String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", lat, lon); + String.format(Locale.ENGLISH, "%1$.6f, %2$.6f", coord.getLatAsDouble(), coord.getLonAsDouble());
updateAppearance(); updateAppearance();
new GeocoderThread(getContext(), lat, lon, new GeocoderThread.Callback() { new GeocoderThread(getContext(), coord, new GeocoderThread.Callback() {
public void onGeocoderResult(final Address address) { public void onGeocoderResult(final Address address) {
if (locationType == LocationType.COORD) { if (locationType == LocationType.COORD) {
setLocation(addressToLocation(address)); setLocation(addressToLocation(address));
@ -365,12 +360,12 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public @Nullable Location getLocation() { public @Nullable Location getLocation() {
final String name = getText(); final String name = getText();
if (locationType == LocationType.COORD && lat == 0 && lon == 0) if (locationType == LocationType.COORD && coord == null)
return null; return null;
else if (locationType == LocationType.ANY && Strings.isNullOrEmpty(name)) else if (locationType == LocationType.ANY && Strings.isNullOrEmpty(name))
return null; return null;
else else
return new Location(locationType, id, Point.fromDouble(lat, lon), name != null ? place : null, name); return new Location(locationType, id, coord, name != null ? place : null, name);
} }
private final OnClickListener contextButtonClickListener = new OnClickListener() { private final OnClickListener contextButtonClickListener = new OnClickListener() {
@ -391,7 +386,7 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
}; };
private void updateAppearance() { private void updateAppearance() {
if (locationType == LocationType.COORD && lat == 0 && lon == 0) if (locationType == LocationType.COORD && coord == null)
leftDrawable.selectDrawableByResId(R.drawable.ic_location_searching_grey600_24dp); leftDrawable.selectDrawableByResId(R.drawable.ic_location_searching_grey600_24dp);
else else
leftDrawable.selectDrawableByResId(LocationView.locationTypeIconRes(locationType)); leftDrawable.selectDrawableByResId(LocationView.locationTypeIconRes(locationType));
@ -425,24 +420,21 @@ public class LocationView extends FrameLayout implements LocationHelper.Callback
public void exchangeWith(final LocationView other) { public void exchangeWith(final LocationView other) {
final LocationType tempLocationType = other.locationType; final LocationType tempLocationType = other.locationType;
final String tempId = other.id; final String tempId = other.id;
final double tempLat = other.lat; final Point tempCoord = other.coord;
final double tempLon = other.lon;
final String tempPlace = other.place; final String tempPlace = other.place;
final String tempText = other.getText(); final String tempText = other.getText();
final String tempHint = other.hint; final String tempHint = other.hint;
other.locationType = this.locationType; other.locationType = this.locationType;
other.id = this.id; other.id = this.id;
other.lat = this.lat; other.coord = this.coord;
other.lon = this.lon;
other.place = this.place; other.place = this.place;
other.setText(this.getText()); other.setText(this.getText());
other.hint = this.hint; other.hint = this.hint;
this.locationType = tempLocationType; this.locationType = tempLocationType;
this.id = tempId; this.id = tempId;
this.lat = tempLat; this.coord = tempCoord;
this.lon = tempLon;
this.place = tempPlace; this.place = tempPlace;
this.setText(tempText); this.setText(tempText);
this.hint = tempHint; this.hint = tempHint;