Skip to content

Commit

Permalink
Show DMS representation of the location
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
pR0Ps committed Jan 19, 2019
1 parent 9e8384e commit 9607c4b
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/ca/cmetcalfe/locationshare/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ private void updateLocation(Location location) {

if (haveLocation) {
String newline = System.getProperty("line.separator");
detailsText.setText(String.format("%s: %s%s%s: %s%s%s: %s",
detailsText.setText(String.format("%s: %s%s%s: %s (%s)%s%s: %s (%s)",
getString(R.string.accuracy), getAccuracy(location), newline,
getString(R.string.latitude), getLatitude(location), newline,
getString(R.string.longitude), getLongitude(location)));
getString(R.string.latitude), getLatitude(location), getDMSLatitude(location), newline,
getString(R.string.longitude), getLongitude(location), getDMSLongitude(location)));

lastLocation = location;
}
Expand Down Expand Up @@ -322,6 +322,26 @@ private String getLatitude(Location location) {
return String.format(Locale.US, "%2.5f", location.getLatitude());
}

private String getDMSLatitude(Location location) {
double val = location.getLatitude();
return String.format(Locale.US, "%.0f° %2.0f′ %2.3f″ %s",
Math.floor(Math.abs(val)),
Math.floor(Math.abs(val * 60) % 60),
(Math.abs(val) * 3600) % 60,
val > 0 ? "N" : "S"
);
}

private String getDMSLongitude(Location location) {
double val = location.getLongitude();
return String.format(Locale.US, "%.0f° %2.0f′ %2.3f″ %s",
Math.floor(Math.abs(val)),
Math.floor(Math.abs(val * 60) % 60),
(Math.abs(val) * 3600) % 60,
val > 0 ? "E" : "W"
);
}

private String getLongitude(Location location) {
return String.format(Locale.US, "%3.5f", location.getLongitude());
}
Expand Down

0 comments on commit 9607c4b

Please sign in to comment.