android 计算gps距离,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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值