Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.

import android.location.Location;

public class Main {
    public static double bearingBetween(Location startLocation, Location endLocation) {
        double lat1 = DegreesToRadians(startLocation.getLatitude());
        double lon1 = DegreesToRadians(startLocation.getLongitude());

        double lat2 = DegreesToRadians(endLocation.getLatitude());
        double lon2 = DegreesToRadians(endLocation.getLongitude());

        double dLon = lon2 - lon1;

        double y = Math.sin(dLon) * Math.cos(lat2);
        double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
        double radiansBearing = Math.atan2(y, x);

        return RadiansToDegrees(radiansBearing);
    }

    static double DegreesToRadians(double degrees) {
        return degrees * Math.PI / 180;
    }

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