Android Calculate Distance Traveled

后端 未结 2 1601
遥遥无期
遥遥无期 2020-12-18 15:02

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 spee

2条回答
  •  -上瘾入骨i
    2020-12-18 15:36

    Try using this: http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html

    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);
    

    Gradle:

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

    EDIT: 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);
    

提交回复
热议问题