get Speed between two Location - Android Map

Android examples for Map:Location

Description

get Speed between two Location

Demo Code


//package com.java2s;
import android.location.Location;

public class Main {
    private static final int earthRadius = 6371;

    public static float getSpeed(Location start, Location end) {
        float distance = calculateDistance(start, end);
        float time = (float) (end.getTime() - start.getTime()) / 1000.0f;
        return distance / time;
    }/*from   w  ww  .j  a  va2  s .  c om*/

    public static float calculateDistance(Location start, Location end) {
        float e = (float) start.getLatitude();
        float f = (float) start.getLongitude();
        float g = (float) end.getLatitude();
        float h = (float) end.getLongitude();

        float dLat = (float) Math.toRadians(g - e);
        float dLon = (float) Math.toRadians(h - f);
        float a = (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math
                .cos(Math.toRadians(e))
                * Math.cos(Math.toRadians(g))
                * Math.sin(dLon / 2) * Math.sin(dLon / 2));
        float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
        float d = earthRadius * c;
        return d * 1000;
    }
}

Related Tutorials