add location formatting method.

This commit is contained in:
madwasp79 2019-07-02 17:53:53 +03:00
parent 35c4853957
commit 48a409da21
3 changed files with 62 additions and 53 deletions

View file

@ -186,17 +186,9 @@ public class PointDescription {
String latLonDeg; String latLonDeg;
String latLonMin; String latLonMin;
String latLonSec; String latLonSec;
String utm;
String olc;
UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon)); String utm = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_UTM);
utm = pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " " + ((long) pnt.northing); String olc = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_OLC);
try {
olc = getLocationOlcName(lat, lon);
} catch (RuntimeException e) {
olc = "0, 0";
}
try { try {
latLonString = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_DEGREES_SHORT); latLonString = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_DEGREES_SHORT);
@ -237,7 +229,6 @@ public class PointDescription {
} else if (f == PointDescription.FORMAT_SECONDS) { } else if (f == PointDescription.FORMAT_SECONDS) {
results.put(LOCATION_LIST_HEADER, latLonSec); results.put(LOCATION_LIST_HEADER, latLonSec);
} }
return results; return results;
} }

View file

@ -1,8 +1,12 @@
package net.osmand.plus; package net.osmand.plus;
import static net.osmand.data.PointDescription.getLocationOlcName;
import android.content.Context; import android.content.Context;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import com.jwetherell.openmap.common.LatLonPoint;
import com.jwetherell.openmap.common.UTMPoint;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.City.CityType; import net.osmand.data.City.CityType;
@ -13,6 +17,7 @@ import net.osmand.osm.PoiType;
import net.osmand.plus.OsmandSettings.AngularConstants; import net.osmand.plus.OsmandSettings.AngularConstants;
import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.OsmandSettings.MetricsConstants;
import net.osmand.plus.OsmandSettings.SpeedConstants; import net.osmand.plus.OsmandSettings.SpeedConstants;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
@ -35,6 +40,21 @@ public class OsmAndFormatter {
private static final SimpleDateFormat SIMPLE_TIME_OF_DAY_FORMAT = new SimpleDateFormat("HH:mm", Locale.getDefault()); private static final SimpleDateFormat SIMPLE_TIME_OF_DAY_FORMAT = new SimpleDateFormat("HH:mm", Locale.getDefault());
private static final String[] localDaysStr = getLettersStringArray(DateFormatSymbols.getInstance().getShortWeekdays(), 3); private static final String[] localDaysStr = getLettersStringArray(DateFormatSymbols.getInstance().getShortWeekdays(), 3);
public static final int FORMAT_DEGREES_SHORT = 100;
public static final int FORMAT_DEGREES = 101;
public static final int FORMAT_MINUTES = 102;
public static final int FORMAT_SECONDS = 103;
public static final int FORMAT_UTM = 104;
public static final int FORMAT_OLC = 105;
private static final char DELIMITER_DEGREES = '°';
private static final char DELIMITER_MINUTES = '';
private static final char DELIMITER_SECONDS = '″';
private static final char NORTH = 'N';
private static final char SOUTH = 'S';
private static final char WEST = 'W';
private static final char EAST = 'E';
{ {
fixed2.setMinimumFractionDigits(2); fixed2.setMinimumFractionDigits(2);
fixed1.setMinimumFractionDigits(1); fixed1.setMinimumFractionDigits(1);
@ -408,61 +428,52 @@ public class OsmAndFormatter {
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
} }
public static final int FORMAT_DEGREES_SHORT = 100;
public static final int FORMAT_DEGREES = 101;
public static final int FORMAT_MINUTES = 102;
public static final int FORMAT_SECONDS = 103;
public static final int FORMAT_UTM = 104;
public static final int FORMAT_OLC = 105;
private static final char DELIMITER_DEGREES = '°';
private static final char DELIMITER_MINUTES = '';
private static final char DELIMITER_SECONDS = '″';
private static final char NORTH = 'N';
private static final char SOUTH = 'S';
private static final char WEST = 'W';
private static final char EAST = 'E';
public static void main(String[] args) {
System.out.println(formatLocationCoordinates(30.122345, -42.101245, FORMAT_DEGREES_SHORT));
System.out.println(formatLocationCoordinates(30.122345, -42.101245, FORMAT_DEGREES));
System.out.println(formatLocationCoordinates(-30.122345, 42.101245, FORMAT_MINUTES));
System.out.println(formatLocationCoordinates(30.122345, -42.101245, FORMAT_SECONDS));
}
public static String formatLocationCoordinates(double lat, double lon, int outputFormat) { public static String formatLocationCoordinates(double lat, double lon, int outputFormat) {
StringBuilder sb = new StringBuilder(); StringBuilder result = new StringBuilder();
if (outputFormat == FORMAT_DEGREES_SHORT) { if (outputFormat == FORMAT_DEGREES_SHORT) {
sb.append(formatCoordinate(lat, outputFormat)).append(" ").append(formatCoordinate(lon, outputFormat)); result.append(formatCoordinate(lat, outputFormat)).append(" ").append(formatCoordinate(lon, outputFormat));
} else if (outputFormat == FORMAT_DEGREES || outputFormat == FORMAT_MINUTES || outputFormat == FORMAT_SECONDS) { } else if (outputFormat == FORMAT_DEGREES || outputFormat == FORMAT_MINUTES || outputFormat == FORMAT_SECONDS) {
sb result
.append(formatCoordinate(lat, outputFormat)) .append(formatCoordinate(lat, outputFormat)).append(" ")
.append(" ") .append(lat > 0 ? NORTH : SOUTH).append(", ")
.append(lat > 0 ? NORTH : SOUTH) .append(formatCoordinate(lon, outputFormat)).append(" ")
.append(", ").append(formatCoordinate(lon, outputFormat))
.append(" ")
.append(lon > 0 ? EAST : WEST); .append(lon > 0 ? EAST : WEST);
} else if (outputFormat == FORMAT_UTM) {
UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon));
result
.append(pnt.zone_number)
.append(pnt.zone_letter).append(" ")
.append((long) pnt.easting).append(" ")
.append((long) pnt.northing);
} else if (outputFormat == FORMAT_OLC) { } else if (outputFormat == FORMAT_OLC) {
String r;
} else if (outputFormat == FORMAT_UTM) { try {
r = getLocationOlcName(lat, lon);
} catch (RuntimeException e) {
r = "0, 0";
}
result.append(r);
} }
return sb.toString(); return result.toString();
} }
private static String formatCoordinate(double coordinate, int outputType) { private static String formatCoordinate(double coordinate, int outputType) {
if (coordinate < -180.0 || coordinate > 180.0 || Double.isNaN(coordinate)) { if (coordinate < -180.0 || coordinate > 180.0 || Double.isNaN(coordinate)) {
throw new IllegalArgumentException("coordinate=" + coordinate); //$NON-NLS-1$ return "Error. Wrong coordinates data!";
} }
if ((outputType != FORMAT_DEGREES) && (outputType != FORMAT_MINUTES) && (outputType if ((outputType != FORMAT_DEGREES) && (outputType != FORMAT_MINUTES) && (outputType
!= FORMAT_SECONDS) && (outputType != FORMAT_DEGREES_SHORT)) { != FORMAT_SECONDS) && (outputType != FORMAT_DEGREES_SHORT)) {
throw new IllegalArgumentException("outputType=" + outputType); //$NON-NLS-1$ return "Unknown Output Format!";
} }
DecimalFormat degDf = new DecimalFormat("##0.00000",new DecimalFormatSymbols(Locale.US));
DecimalFormat minDf = new DecimalFormat("00.0000",new DecimalFormatSymbols(Locale.US));
DecimalFormat secDf = new DecimalFormat("00.000",new DecimalFormatSymbols(Locale.US));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// Handle negative values
if (coordinate < 0) { if (coordinate < 0) {
if (outputType == FORMAT_DEGREES_SHORT) { if (outputType == FORMAT_DEGREES_SHORT) {
sb.append('-'); sb.append('-');
@ -470,9 +481,6 @@ public class OsmAndFormatter {
coordinate = -coordinate; coordinate = -coordinate;
} }
DecimalFormat degDf = new DecimalFormat("##0.00000",new DecimalFormatSymbols(Locale.US));
DecimalFormat minDf = new DecimalFormat("00.0000",new DecimalFormatSymbols(Locale.US));
DecimalFormat secDf = new DecimalFormat("00.000",new DecimalFormatSymbols(Locale.US));
if (outputType == FORMAT_DEGREES_SHORT) { if (outputType == FORMAT_DEGREES_SHORT) {
sb.append(degDf.format(coordinate)); sb.append(degDf.format(coordinate));
} else if (outputType == FORMAT_DEGREES) { } else if (outputType == FORMAT_DEGREES) {

View file

@ -17,9 +17,11 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.view.ContextThemeWrapper; import android.support.v7.view.ContextThemeWrapper;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -681,7 +683,15 @@ public class MenuBuilder {
LinearLayout llv = buildCollapsableContentView(mapActivity, true, true); LinearLayout llv = buildCollapsableContentView(mapActivity, true, true);
for (final Map.Entry<Integer, String> line : locationData.entrySet()) { for (final Map.Entry<Integer, String> line : locationData.entrySet()) {
final TextViewEx button = buildButtonInCollapsableView(mapActivity, false, false); final TextViewEx button = buildButtonInCollapsableView(mapActivity, false, false);
button.setText(line.getValue()); if (line.getKey() == OsmAndFormatter.FORMAT_UTM || line.getKey() == OsmAndFormatter.FORMAT_OLC) {
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(line.getKey() == OsmAndFormatter.FORMAT_UTM ? "UTM: " : "OLC: ");
ssb.setSpan(new ForegroundColorSpan(app.getResources().getColor(R.color.text_color_secondary_light)), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.append(line.getValue());
button.setText(ssb);
} else {
button.setText(line.getValue());
}
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {