latlong format
This commit is contained in:
parent
6dabd11dba
commit
35c4853957
5 changed files with 149 additions and 27 deletions
|
@ -11,6 +11,7 @@ import com.jwetherell.openmap.common.UTMPoint;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import net.osmand.LocationConvert;
|
||||
import net.osmand.plus.OsmAndFormatter;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -52,6 +53,8 @@ public class PointDescription {
|
|||
public static final String POINT_TYPE_MAPILLARY_IMAGE = "mapillary_image";
|
||||
public static final String POINT_TYPE_POI_TYPE = "poi_type";
|
||||
public static final String POINT_TYPE_CUSTOM_POI_FILTER = "custom_poi_filter";
|
||||
public static final int LOCATION_URL = 200;
|
||||
public static final int LOCATION_LIST_HEADER = 201;
|
||||
|
||||
|
||||
public static final PointDescription LOCATION_POINT = new PointDescription(POINT_TYPE_LOCATION, "");
|
||||
|
@ -175,41 +178,66 @@ public class PointDescription {
|
|||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> getLocationData(MapActivity ctx, double lat, double lon, boolean sh) {
|
||||
public static Map<Integer, String> getLocationData(MapActivity ctx, double lat, double lon, boolean sh) {
|
||||
OsmandSettings settings = ((OsmandApplication) ctx.getApplicationContext()).getSettings();
|
||||
Map<String, String> results = new LinkedHashMap<>();
|
||||
|
||||
int f = settings.COORDINATES_FORMAT.get();
|
||||
Map<Integer, String> results = new LinkedHashMap<>();
|
||||
|
||||
String latLonString ;
|
||||
String latLonDeg;
|
||||
String latLonMin;
|
||||
String latLonSec;
|
||||
String utm;
|
||||
String olc;
|
||||
|
||||
UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon));
|
||||
results.put(PointDescription.formatToHumanString(ctx, UTM_FORMAT), pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " "+ ((long) pnt.northing));
|
||||
utm = pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " " + ((long) pnt.northing);
|
||||
|
||||
try {
|
||||
results.put(PointDescription.formatToHumanString(ctx, OLC_FORMAT), getLocationOlcName(lat, lon));
|
||||
olc = getLocationOlcName(lat, lon);
|
||||
} catch (RuntimeException e) {
|
||||
results.put(PointDescription.formatToHumanString(ctx, OLC_FORMAT), "0, 0");
|
||||
olc = "0, 0";
|
||||
}
|
||||
|
||||
if (f == PointDescription.UTM_FORMAT || f == PointDescription.OLC_FORMAT) {
|
||||
f = PointDescription.FORMAT_DEGREES;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
results.put(PointDescription.formatToHumanString(ctx, f),
|
||||
ctx.getString(sh ? R.string.short_location_on_map : R.string.location_on_map, LocationConvert.convert(lat, f),
|
||||
LocationConvert.convert(lon, f)));
|
||||
latLonString = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_DEGREES_SHORT);
|
||||
latLonDeg = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_DEGREES);
|
||||
latLonMin = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_MINUTES);
|
||||
latLonSec = OsmAndFormatter.formatLocationCoordinates(lat, lon, OsmAndFormatter.FORMAT_SECONDS);
|
||||
} catch (RuntimeException e) {
|
||||
e.printStackTrace();
|
||||
results.put(PointDescription.formatToHumanString(ctx, f),
|
||||
ctx.getString(sh ? R.string.short_location_on_map : R.string.location_on_map, 0, 0));
|
||||
latLonString = "0, 0";
|
||||
latLonDeg = "0°, 0°";
|
||||
latLonMin = "0° 0′, 0° 0′";
|
||||
latLonSec = "0° 0′ 0″, 0° 0′ 0″";
|
||||
}
|
||||
|
||||
results.put(OsmAndFormatter.FORMAT_DEGREES_SHORT, latLonString);
|
||||
results.put(OsmAndFormatter.FORMAT_DEGREES, latLonDeg);
|
||||
results.put(OsmAndFormatter.FORMAT_MINUTES, latLonMin);
|
||||
results.put(OsmAndFormatter.FORMAT_SECONDS, latLonSec);
|
||||
results.put(OsmAndFormatter.FORMAT_UTM, utm);
|
||||
results.put(OsmAndFormatter.FORMAT_OLC, olc);
|
||||
|
||||
int zoom = 17;
|
||||
if (ctx.getMapView() != null) {
|
||||
zoom = ctx.getMapView().getZoom();
|
||||
}
|
||||
final String httpUrl = "https://osmand.net/go?lat=" + (lat)
|
||||
+ "&lon=" + (lon) + "&z=" + zoom;
|
||||
results.put("URL", httpUrl);
|
||||
final String httpUrl = "https://osmand.net/go?lat=" + (lat) + "&lon=" + (lon) + "&z=" + zoom;
|
||||
results.put(LOCATION_URL, httpUrl);
|
||||
|
||||
int f = settings.COORDINATES_FORMAT.get();
|
||||
|
||||
if (f == PointDescription.UTM_FORMAT) {
|
||||
results.put(LOCATION_LIST_HEADER, utm);
|
||||
} else if (f == PointDescription.OLC_FORMAT) {
|
||||
results.put(LOCATION_LIST_HEADER, olc);
|
||||
} else if (f == PointDescription.FORMAT_DEGREES) {
|
||||
results.put(LOCATION_LIST_HEADER, latLonDeg);
|
||||
} else if (f == PointDescription.FORMAT_MINUTES) {
|
||||
results.put(LOCATION_LIST_HEADER, latLonMin);
|
||||
} else if (f == PointDescription.FORMAT_SECONDS) {
|
||||
results.put(LOCATION_LIST_HEADER, latLonSec);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus;
|
|||
import android.content.Context;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import net.osmand.data.Amenity;
|
||||
import net.osmand.data.City.CityType;
|
||||
import net.osmand.osm.AbstractPoiType;
|
||||
|
@ -407,4 +408,95 @@ public class OsmAndFormatter {
|
|||
cal1.get(Calendar.YEAR) == cal2.get(Calendar.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) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (outputFormat == FORMAT_DEGREES_SHORT) {
|
||||
sb.append(formatCoordinate(lat, outputFormat)).append(" ").append(formatCoordinate(lon, outputFormat));
|
||||
} else if (outputFormat == FORMAT_DEGREES || outputFormat == FORMAT_MINUTES || outputFormat == FORMAT_SECONDS) {
|
||||
sb
|
||||
.append(formatCoordinate(lat, outputFormat))
|
||||
.append(" ")
|
||||
.append(lat > 0 ? NORTH : SOUTH)
|
||||
.append(", ").append(formatCoordinate(lon, outputFormat))
|
||||
.append(" ")
|
||||
.append(lon > 0 ? EAST : WEST);
|
||||
} else if (outputFormat == FORMAT_OLC) {
|
||||
|
||||
} else if (outputFormat == FORMAT_UTM) {
|
||||
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String formatCoordinate(double coordinate, int outputType) {
|
||||
if (coordinate < -180.0 || coordinate > 180.0 || Double.isNaN(coordinate)) {
|
||||
throw new IllegalArgumentException("coordinate=" + coordinate); //$NON-NLS-1$
|
||||
}
|
||||
if ((outputType != FORMAT_DEGREES) && (outputType != FORMAT_MINUTES) && (outputType
|
||||
!= FORMAT_SECONDS) && (outputType != FORMAT_DEGREES_SHORT)) {
|
||||
throw new IllegalArgumentException("outputType=" + outputType); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// Handle negative values
|
||||
if (coordinate < 0) {
|
||||
if (outputType == FORMAT_DEGREES_SHORT) {
|
||||
sb.append('-');
|
||||
}
|
||||
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) {
|
||||
sb.append(degDf.format(coordinate));
|
||||
} else if (outputType == FORMAT_DEGREES) {
|
||||
sb.append(degDf.format(coordinate)).append(DELIMITER_DEGREES);
|
||||
} else if (outputType == FORMAT_MINUTES) {
|
||||
sb.append(minDf.format(formatCoordinate(coordinate, sb, DELIMITER_DEGREES)))
|
||||
.append(DELIMITER_MINUTES);
|
||||
} else {
|
||||
sb.append(secDf.format(formatCoordinate(
|
||||
formatCoordinate(coordinate, sb, DELIMITER_DEGREES), sb, DELIMITER_MINUTES)))
|
||||
.append(DELIMITER_SECONDS);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static double formatCoordinate(double coordinate, StringBuilder sb, char delimiter) {
|
||||
int deg = (int) Math.floor(coordinate);
|
||||
if (deg < 10) {
|
||||
sb.append('0');
|
||||
}
|
||||
sb.append(deg);
|
||||
sb.append(delimiter);
|
||||
coordinate -= deg;
|
||||
coordinate *= 60.0;
|
||||
return coordinate;
|
||||
}
|
||||
}
|
|
@ -677,9 +677,9 @@ public class MenuBuilder {
|
|||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
protected CollapsableView getLocationCollapsableView(Map<String, String> locationData) {
|
||||
protected CollapsableView getLocationCollapsableView(Map<Integer, String> locationData) {
|
||||
LinearLayout llv = buildCollapsableContentView(mapActivity, true, true);
|
||||
for (final Map.Entry<String, String> line : locationData.entrySet()) {
|
||||
for (final Map.Entry<Integer, String> line : locationData.entrySet()) {
|
||||
final TextViewEx button = buildButtonInCollapsableView(mapActivity, false, false);
|
||||
button.setText(line.getValue());
|
||||
button.setOnClickListener(new OnClickListener() {
|
||||
|
|
|
@ -289,7 +289,7 @@ public abstract class MenuController extends BaseMenuController implements Colla
|
|||
final MapActivity mapActivity = getMapActivity();
|
||||
if (mapActivity != null) {
|
||||
String f = PointDescription.formatToHumanString(mapActivity, mapActivity.getMyApplication().getSettings().COORDINATES_FORMAT.get());
|
||||
Map<String, String> locationData = PointDescription.getLocationData(mapActivity, latlon.getLatitude(), latlon.getLongitude(), true);
|
||||
Map<Integer, String> locationData = PointDescription.getLocationData(mapActivity, latlon.getLatitude(), latlon.getLongitude(), true);
|
||||
CollapsableView cv = builder.getLocationCollapsableView(locationData);
|
||||
addPlainMenuItem(R.drawable.ic_action_get_my_location, locationData.get(f).replace("\n", " "), true, false, true, cv, null);
|
||||
}
|
||||
|
|
|
@ -673,10 +673,12 @@ public class AmenityMenuBuilder extends MenuBuilder {
|
|||
0, false, null, true, 0, true, null, false);
|
||||
}
|
||||
|
||||
String f = PointDescription.formatToHumanString(mapActivity, app.getSettings().COORDINATES_FORMAT.get());
|
||||
Map<String, String> locationData = PointDescription.getLocationData(mapActivity, amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(), true);
|
||||
Map<Integer, String> locationData = PointDescription.getLocationData(mapActivity, amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(), true);
|
||||
String title = locationData.get(PointDescription.LOCATION_LIST_HEADER);
|
||||
locationData.remove(PointDescription.LOCATION_LIST_HEADER);
|
||||
CollapsableView cv = getLocationCollapsableView(locationData);
|
||||
buildRow(view, R.drawable.ic_action_get_my_location, null, locationData.get(f).replace("\n", " "), 0, true, cv,
|
||||
|
||||
buildRow(view, R.drawable.ic_action_get_my_location, null, title, 0, true, cv,
|
||||
true, 1, false, null, false);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue