Android:如何使用GPS测量距离

3

我正在制作一个应用程序,它可以获取用户当前位置的纬度和经度,然后计算用户移动的瞬时距离(单位为千米)。

"currentLat"和"currentLon"是用户当前位置的纬度和经度。但是我不知道要将哪些纬度和经度放入"endLat"和"endLon"中。

对于我的糟糕英语,我感到很抱歉。

提前感谢。

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

我已经制作了这个应用程序,但现在只有一个小问题。 当我第一次启动程序时,我得到值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米。 - Carnal
3个回答

4

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

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

位置1是用户的位置,但位置2会是什么呢?这是我最大的问题。 - cross_fire
@Carnal 这不是问题,也不是那个问题的任何8个修订版本中的问题。而且谁会为一个没有被问到的问题点赞呢?你是自己点赞的吗(带点小帮助)? - AlexWien

1

但是我不知道在“endLat”和“endLon”中放置什么纬度和经度。

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

应该有lastLat,lastLon:上一次调用onGpsUpdate时接收到的纬度,经度,将其保存到你的distanceCalculator.lastLat中,其中distanceCalculator是一个对象,你在其中存储了lastLat和lastLon

然后你应该有curLat,curLon:onGPSUpdate提供的当前纬度,经度。

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


0

链接

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.

这不是问题,也不是那个问题的8个修订版本中的任何一个。而且谁会为一个没有被问到的问题点赞呢?你是自己给自己点赞的吗(带点小帮助)? - AlexWien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接