2014-05-23 36 views
0

我完整的代碼
https://gist.github.com/anonymous/6dd0f33270cc4f46149egetLastKnownLocation無法通過location.setLatitude/setLongitude獲取新數據?

在線路110〜131我要改變位置,它的工作,的MapView將改變

private class MapClickedListener implements OnClickListener { 

    @Override 
    public void onClick (View v) { 
     String lng = "121.558561"; 
     lng = edt_lng.getText().toString().trim(); 
     String lat = "25.031005"; 
     lat = edt_lat.getText().toString().trim(); 
     if(lng.equals("")||lat.equals("")){ 
      Toast.makeText(getApplicationContext(), "input again!", Toast.LENGTH_LONG).show(); 
      location = locManager.getLastKnownLocation(bestProvider); 
      updateToNewLocation(location); 
     }else{ 
      Location location = new Location(LocationManager.NETWORK_PROVIDER); 
      location.setLongitude(Double.parseDouble(lng)); 
      location.setLatitude(Double.parseDouble(lat)); 
      updateToNewLocation(location); 
     } 

    } 

} 

線133〜148,我想知道的距離之間位置和點我設置

private Button.OnClickListener EQ = new Button.OnClickListener(){ 
    @Override 
    public void onClick (View v) { 
     eqlocation.setLongitude(120.82); 
     eqlocation.setLatitude(23.85); 
     location = locManager.getLastKnownLocation(bestProvider); 
     float[] result = new float[5]; 
     Location.distanceBetween(location.getLatitude(), location.getLongitude(), eqlocation.getLatitude(), eqlocation.getLongitude(), result); 
     BigDecimal bd = new BigDecimal(result[0]); 
     BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP); 
     double dis = rounded.doubleValue(); 
     String dist = String.valueOf(dis/1000); 
     Toast.makeText(getApplicationContext(), "distance: " + dist + "km", Toast.LENGTH_LONG).show(); 
    } 

}; 

但是,當我通過第110〜131行的代碼更改位置時,結果是錯誤的。
我該怎麼做才能獲得正確的結果?

回答

0

你有沒有嘗試distanceTo()並檢查,如果你得到任何更好的結果?,我ususally使用distanceTo()與良好的結果。