Calculate the amount of degrees of longitude for a given distance in meters. - Android Map

Android examples for Map:Latitude Longitude

Description

Calculate the amount of degrees of longitude for a given distance in meters.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*w w w  .  ja  v a 2s  .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
 ******************************************************************************/
import static java.lang.Math.cos;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.Math.tan;
import java.util.List;
import android.location.Location;

public class Main{
    /**
     * The multiplication factor to convert from double to int.
     */
    private static final double FACTOR_DOUBLE_TO_INT = 1000000;
    /**
     * Calculate the amount of degrees of longitude for a given distance in meters.
     * 
     * @param meters
     *            distance in meters
     * @param latitude
     *            the latitude at which the calculation should be performed
     * @return longitude degrees
     */
    public static double longitudeDistance(int meters, double latitude) {
        return (meters * 360)
                / (2 * Math.PI * LocationConstants.EQUATORIALRADIUS * Math
                        .cos(Math.toRadians(latitude)));
    }
    /**
     * Calculate the amount of degrees of longitude for a given distance in meters.
     * 
     * @param meters
     *            distance in meters
     * @param latitude
     *            the latitude at which the calculation should be performed
     * @return longitude degrees
     */
    public static double longitudeDistance(int meters, int latitude) {
        return (meters * 360)
                / (2 * Math.PI * LocationConstants.EQUATORIALRADIUS * Math
                        .cos(Math.toRadians(intToDouble(latitude))));
    }
    /**
     * Converts a coordinate from microdegrees to degrees.
     * 
     * @param coordinate
     *            the coordinate in microdegrees.
     * @return the coordinate in degrees.
     */
    public static double intToDouble(int coordinate) {
        return coordinate / FACTOR_DOUBLE_TO_INT;
    }
}

Related Tutorials