本文介绍了Android计算行进距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究适合健身的应用程序.我的基本想法是允许人们跟踪他们的速度,距离,时间.到目前为止,我已经设法通过使用位置管理器getSpeed()来加快工作速度.我想找出如何获得行驶距离?我正在寻找一些示例,但是当我刚开始使用android时,这让我有些困惑.我将不胜感激,谢谢

I am currently working on an application focused around fitness. The basic I idea is to allow to people to track their speed,distance,time. So far I have managed to get speed working by using location manager getSpeed(). I wanted to find out how I can get the distance traveled?I looked for some samples but was bit confusing for me as I just started to work with android.I would appreciate any help or advise , thanks

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

    start = (Button) findViewById(R.id.btnStart);
    speed = (TextView)findViewById(R.id.txtSpeed);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);



    //initialize location listener
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            getSpeed(location);
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }

        //get the speed from the given location updates
        public void getSpeed(Location location) {
            currentSpeed = (location.getSpeed()*3600/1000);
            String convertedSpeed = String.format("%.2f",currentSpeed);
            speed.setText(convertedSpeed + "Km/h");
        }

        public void getDistance(Location location){

        }

    };

推荐答案

尝试使用此方法: http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html

对于获得的每个位置,创建一个LatLng并将其添加到ArrayList中.然后使用此功能可以为您提供正确的长度:

For each location you get, you create a LatLng and added it to a ArrayList.Then with this function it gets you the correct length:

SphericalUtil.computeLength(latLngs);

等级:

 dependencies {
compile 'com.google.maps.android:android-maps-utils:0.4+'
}

您有一个Arraylist位置=全局为新的ArrayList.然后在"onLocationChanged"中添加位置:

You have an Arraylist locations = new ArrayList globally.And in your "onLocationChanged" you add to the locations:

  @Override
    public void onLocationChanged(Location location) {
        locations.add(location);
    }

然后在需要时执行以下操作:

Then when needed you do:

 for (Location loc :  locations){
     latLngs.add(new LatLng(loc.getLocation().getLat(), loc.getLocation().getLng()));
 }
 Double dist = SphericalUtil.computeLength(latLngs);

这篇关于Android计算行进距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:57