Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android location to string issue

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);
like image 462
shkschneider Avatar asked Aug 05 '26 09:08

shkschneider


1 Answers

You can use public static String convert (double coordinate, int outputType) from Location Class. The outputType can be one of FORMAT_DEGREES, FORMAT_MINUTES, or FORMAT_SECONDS.

public static String locationStringFromLocation(final Location location) {
    return Location.convert(location.getLatitude(), Location.FORMAT_DEGREES) + " " + Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
}
like image 166
Tudor Luca Avatar answered Aug 06 '26 21:08

Tudor Luca