Calculate middle Location - Android Map

Android examples for Map:Location

Description

Calculate middle Location

Demo Code


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

public class Main {
    public static Location middleLocation(Location start, Location end,
            float meters) {
        double lat1 = toRad(start.getLatitude());
        double lon1 = toRad(start.getLongitude());
        double R = 6371; // radius of earth in km
        double d = meters / 1000; // in km
        float brng = (float) (toRad(start.bearingTo(end)));
        double lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R)
                + Math.cos(lat1) * Math.sin(d / R) * Math.cos(brng));
        double lon2 = lon1
                + Math.atan2(/*from   ww  w.  j  a va  2  s.  c o  m*/
                        Math.sin(brng) * Math.sin(d / R) * Math.cos(lat1),
                        Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2));
        Location nl = new Location(start);
        nl.setLatitude(toDegree(lat2));
        nl.setLongitude(toDegree(lon2));
        nl.setBearing(brng);
        return nl;
    }

    private static double toRad(double degree) {
        return degree * Math.PI / 180;
    }

    private static double toDegree(double radians) {
        return radians * 180 / Math.PI;
    }
}

Related Tutorials