Android:如何用gps测量距离

时间:2013-04-05 07:33:21

标签: android gps distance measure

我正在尝试创建一个应用程序,它在用户的当前位置取纬度和经度。并计算瞬间,用户以km为单位的距离。

“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



}

 };

}

3 个答案:

答案 0 :(得分:3)

由于您有两个位置,为什么不考虑使用distanceTo

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

答案 1 :(得分:1)

  

但我不知道“endLat”和“ednLon”的经度和经度是什么。

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

你应该有lastLat,lastLon:在之前的onGpsUpdate调用中收到的lat,lon,将它保存到例如distanceCalculator.lastLat中 其中distanceCalculator是存储lastLat和lastLon

的对象

然后你应该有curLat,curLon:当前lat,lon在onGPSUpdate中传递

然后计算距离(lastLat,lastLon) - > (curLat,curLon), 并更新你的distanceCalculator.lastLat和lastLon。

答案 2 :(得分: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.