如何将Android获取的经纬度精度提升至小数点后15位?
Hey there! I see you're trying to get more precision out of your GPS coordinates—let's fix that. The issue isn't with the getLatitude() or getLongitude() methods themselves (they return a double which can hold ~15-17 significant digits), but with how the values are being converted to a string by default. When you concatenate the double directly into a string, it only shows up to 7 decimal places.
Here's how to display 15 decimal places:
We can use either String.format() or DecimalFormat to explicitly set the number of decimal places. Let's adjust your onLocationChanged method with both options:
Option 1: Using String.format()
This is the simpler approach for quick formatting:
@Override public void onLocationChanged(Location location) { // Format latitude and longitude to 15 decimal places String preciseLatitude = String.format(Locale.getDefault(), "%.15f", location.getLatitude()); String preciseLongitude = String.format(Locale.getDefault(), "%.15f", location.getLongitude()); // Update the text view with precise coordinates locationText.setText("Latitude: " + preciseLatitude + "\n Longitude: " + preciseLongitude); try { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); locationText.setText(locationText.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+ addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2)); } catch(Exception e) { e.printStackTrace(); // Don't leave catch blocks empty—this helps debug issues! } }
Option 2: Using DecimalFormat
If you need more control over formatting (like trimming trailing zeros), use this:
import java.text.DecimalFormat; @Override public void onLocationChanged(Location location) { // Create a formatter for 15 decimal places DecimalFormat decimalFormat = new DecimalFormat("#.###############"); decimalFormat.setMaximumFractionDigits(15); decimalFormat.setMinimumFractionDigits(15); String preciseLatitude = decimalFormat.format(location.getLatitude()); String preciseLongitude = decimalFormat.format(location.getLongitude()); locationText.setText("Latitude: " + preciseLatitude + "\n Longitude: " + preciseLongitude); // Address retrieval code remains the same... try { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); locationText.setText(locationText.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+ addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2)); } catch(Exception e) { e.printStackTrace(); } }
Bonus tip for better precision:
Your current code uses NETWORK_PROVIDER, which is faster but less precise than GPS. If you need the highest possible accuracy (which is required to actually get 15 decimal places of meaningful data), add the GPS provider to your location requests:
void getLocation() { try { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Request updates from both GPS and Network providers locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this); } catch(SecurityException e) { e.printStackTrace(); } }
Just make sure you have the ACCESS_FINE_LOCATION permission granted (which you already request in your code).
Keep in mind: Even with these changes, the actual precision you get depends on your device's GPS hardware. Most consumer GPS modules can only provide accuracy down to a few centimeters, so the last few decimal places might not be meaningful—but at least you'll be displaying all the precision that's available from the Location object.
内容的提问来源于stack exchange,提问作者mach2

