3

Here is the code I use the put locations to strings:

public static String locationStringFromLocation(final Location location) {
    return String.format("%.3f %.3f", location.getLatitude(), location.getLongitude());
}

And from some other devices, from time to time I get: -7.2900002123788E-4 7.270000060088933E-4 as location string and not -7.290 7.270 for example.

  • Does someone has clue on this?
  • How to improve my code?

Edit

Updated code. Will this fix the issue?

DecimalFormat decimalFormat = new DecimalFormat("#.###");
if (location != null) {
    final String latitude = decimalFormat.format(Float.valueOf(Location.convert(location.getLatitude(), Location.FORMAT_DEGREES)));
    final String longitude = decimalFormat.format(Float.valueOf(Location.convert(location.getLongitude(), Location.FORMAT_DEGREES)));
    return latitude + " " + longitude;
}
return decimalFormat.format(0.0F) + " " + decimalFormat.format(0.0F);
4

2 回答 2

5

您可以public static String convert (double coordinate, int outputType)从位置类使用。outputType 可以是FORMAT_DEGREESFORMAT_MINUTES或之一FORMAT_SECONDS

public static String locationStringFromLocation(final Location location) {
    return Location.convert(location.getLatitude(), Location.FORMAT_DEGREES) + " " + Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
}
于 2012-12-11T10:38:06.057 回答
0

一行,一种方法,方便的解决方法:

String.substring(beginIndex,finalIndex)

所以在你的情况下:

latInDecimal = lat.substring(0,5)
lngInDecimal = lng.substring(0,5)
于 2019-09-10T22:22:25.650 回答