用distanceto显示距离

时间:2018-05-26 17:27:04

标签: java android distance

首先抱歉我的英语不好。我无法显示手机中的距离,这是我的代码,我找不到错误。我认为这可能是权限

Location mCurrentLocation, lStart, lEnd;
float dist=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    this.onLocationChanged(null);
}

public void onLocationChanged(Location location) {
        mCurrentLocation=location;
    if(lStart==null){
        lStart=lEnd=mCurrentLocation;
    }else{
        lEnd=mCurrentLocation;
    }
    updateUI();
}

private void updateUI(){
    TextView txt2 = (TextView) this.findViewById(R.id.textView2);
        dist = dist + (lStart.distanceTo(lEnd));
        txt2.setText(dist + "m");
        lStart = lEnd;
}

0 个答案:

没有答案