get Distance by latitude and latitude - Android Map

Android examples for Map:Latitude Longitude

Description

get Distance by latitude and latitude

Demo Code


//package com.java2s;

public class Main {
    private static final double a = 6378137D;
    private static final double f = 1D / 298.257222101D;

    public static double getDistance(double latitude1, double longitude1,
            double latitude2, double longitude2) {

        double radLatStart = latitude1 * Math.PI / 180D;
        double radLonStart = longitude1 * Math.PI / 180D;
        double radLatEnd = latitude2 * Math.PI / 180D;
        double radLonEnd = longitude2 * Math.PI / 180D;

        double avgLat = (radLatStart + radLatEnd) / 2D;

        ////  w  w  w.  j  a  v a2s.c  o m
        double F = 1D / f;

        //
        double e = (Math.sqrt(2 * F - 1)) / F;
        double W = Math.sqrt(1 - Math.pow(e, 2)
                * Math.pow(Math.sin(avgLat), 2));

        //
        double M = (a * (1 - Math.pow(e, 2))) / Math.pow(W, 3);

        //
        double N = a / W;

        //2()    
        double dLat = radLatStart - radLatEnd;

        //2()
        double dLon = radLonStart - radLonEnd;

        //2
        double d = Math.sqrt(Math.pow(M * dLat, 2)
                + Math.pow(N * Math.cos(avgLat) * dLon, 2));

        return d;
    }
}

Related Tutorials