2014-01-17 64 views
0

我對我的android應用程序有點麻煩。我想使用手機的GPS顯示當前的經緯度。我能夠獲得手機的當前座標,但它顯示爲一個雙倍數值...我認爲小數點後6-8位。有沒有辦法減少小數點後的位數?有沒有辦法減少小數點後的位數?

這裏是我的代碼:

public class Localisation extends Activity implements LocationListener{ 

protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Context context; 
private String TripDescription; 
private String tripID; 



TextView txtLat; 
String lat; 
String provider; 
protected String latitude, longitude; 

protected boolean gps_enabled, network_enabled; 


@Override 

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_localisation); 
    txtLat = (TextView) findViewById(R.id.textView1); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

} 


@Override 
public void onLocationChanged(Location location) { 
    txtLat = (TextView) findViewById(R.id.textView1); 
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 

} 

@Override 
public void onProviderDisabled(String arg0) { 
    Log.d("Latitude","disable"); 

} 

@Override 
public void onProviderEnabled(String provider) { 
    Log.d("Latitude","enable");  
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    Log.d("Latitude","status");  
} 
+1

考慮我的答案upvote,這是正確的。 – AlexWien

回答

1

爲了減少(float或double),以6個位數:

double oldVal = location.getLatitude(); 
double newVal = ((int) Math.round(oldVal * 1E6))/1E6; 

乘法一百萬截斷6位數後的一切。 分隔後,值限制爲6位數。

+0

適用於此。謝謝。 – statosdotcom

相關問題