2013-04-05 105 views
3

我正在嘗試製作一個應用程序,它將用戶的當前位置的緯度和經度作爲參考。並且瞬間計數,以km爲單位的用戶旅行距離。Android:如何使用GPS測量距離

「currentLat」和「currentLon」是當前用戶的緯度和經度。但我不知道放入「endLat」和「ednLon」的經度和緯度。

對不起,我的英語不好。

在此先感謝。

///////////////////////////////////

我做它的應用程序,但現在只有一個小問題。 當我第一次開始程序,我得到價值5536,當我重新啓動程序,我得到正常值0.0

再次抱歉我的英語不好。 :)

和球員,感謝幫助我,你是最好的:)

public class Gps extends Activity { 

TextView display; 


    double currentLon=0 ; 
    double currentLat=0 ; 
    double lastLon = 0; 
    double lastLat = 0; 
    double distance; 




public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 




    display = (TextView) findViewById(R.id.info); 

    LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
       lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist); 
       Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

       if(loc==null){ 
        display.setText("No GPS location found"); 
        } 
        else{ 
         //set Current latitude and longitude 
         currentLon=loc.getLongitude(); 
         currentLat=loc.getLatitude(); 

         } 
       //Set the last latitude and longitude 
       lastLat=currentLat; 
       lastLon=currentLon ; 


} 



LocationListener Loclist = new LocationListener(){ 




@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 

    //start location manager 
    LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 

     //Get last location 
    Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

    //Request new location 
     lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist); 

     //Get new location 
     Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER); 

     //get the current lat and long 
    currentLat = loc.getLatitude(); 
    currentLon = loc.getLongitude(); 


    Location locationA = new Location("point A"); 
     locationA.setLatitude(lastLat); 
     locationA.setLongitude(lastLon); 

    Location locationB = new Location("point B"); 
     locationB.setLatitude(currentLat); 
     locationB.setLongitude(currentLon); 

     double distanceMeters = locationA.distanceTo(locationB); 

     double distanceKm = distanceMeters/1000f; 

     display.setText(String.format("%.2f Km",distanceKm)); 

     } 



@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 



@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 



@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 



} 

}; 

} 
+0

這裏有什麼錯誤? – 2013-04-05 07:40:26

+0

順便說一句,你的方法發送相同的位置,所以距離總是會返回0米。 – Carnal 2013-04-05 07:44:53

回答

1

但是我不知道放入「endLat」和「ednLon」的經緯度。

endLat,endLon是一個令人困惑的名字:

你應該有lastLat,lastLon:拉特,LON在onGpsUpdate先前接收的呼叫,保存到你的如distanceCalculator.lastLat 其中distanceCalculator是物體在那裏你存儲lastLat和lastLon

那麼你應該有curLat,curLon:當前的緯度,經度在onGPSUpdate

交付然後計算從(lastLat,lastLon)距離 - >(curLat,curLon) 並更新您的distanceCalculator.lastLat和lastLon。

3

既然你有兩個地點,你爲什麼不考慮使用distanceTo

float distanceMeters = location1.distanceTo(location2); 
float distanceKm = distanceMeters/1000f; 
+0

location1是用戶的位置,但是什麼將location2.This是我最大的問題 – 2013-04-05 08:01:10

+0

@Carnal這不是問題,沒有在這個問題的8個修訂。對於一個沒有被問到的問題,誰做了贊成?你是否贊成自己(有一點幫助)? – AlexWien 2013-04-05 13:10:14

0

Link

var R = 6371; // Radius of the earth in km 
var dLat = (lat2-lat1).toRad(); // Javascript functions in radians 
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
     Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
     Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; // Distance in km. 
+0

這不是問題,也不是這個問題的8個修訂版本。對於一個沒有被問到的問題,誰做了贊成?你是否贊成自己(有一點幫助)? – AlexWien 2013-04-05 13:09:45