Get the GPS bearing for given GPS points - Android Map

Android examples for Map:GPS

Description

Get the GPS bearing for given GPS points

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from   w  w w . java  2  s.c  o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Get the GPS bearing for given GPS points
     * 
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @return
     */
    public static double getBearing(double lon1, double lat1, double lon2,
            double lat2) {
        double dLon = Math.toRadians((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);
        return Math.toDegrees(Math.atan2(y, x));
    }
}

Related Tutorials