Convert UTM coordinates to GPS coordinates - Android Map

Android examples for Map:GPS

Description

Convert UTM coordinates to GPS coordinates

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from w ww  .  j  av  a  2  s.  co 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{
    /**
     * Convert UTM coordinates to GPS coordinates
     * 
     * @param utm
     * @return
     */
    public static GPSCoordinate getGPSCoordinate(UTMCoordinate utm) {
        int x = (utm.getX() - 340000000) / 10;
        int y = (utm.getY() - 130000000) / 10;

        double M0 = LocationConstants.UTM_CURVATURE;
        double M = M0
                + ((y - LocationConstants.UTMK_HEIGHT) / LocationConstants.UTMK_SCALE);
        double u1 = M
                / (LocationConstants.E_R_WGS84 * E1F(LocationConstants.UTM_HOR));

        double temp = sqrt(1. - LocationConstants.UTM_HOR);
        double e1 = (1 - temp) / (1 + temp);

        double v1 = (((3. * e1) / 2.) - ((27. * pow(e1, 3.)) / 32.))
                * sin(2. * u1);
        double v2 = (((21. * pow(e1, 2.)) / 16.) - ((55. * pow(e1, 4.)) / 32.))
                * sin(4. * u1);
        double v3 = ((151. * pow(e1, 3.)) / 96.) * sin(6. * u1);
        double v4 = ((1097. * pow(e1, 4.)) / 512.) * sin(8. * u1);

        double q1 = u1 + v1 + v2 + v3 + v4;

        double sq1 = sin(q1);
        double cq1 = cos(q1);
        double tq1 = tan(q1);

        double N1 = LocationConstants.E_R_WGS84
                / sqrt(1. - LocationConstants.UTM_HOR * (sq1 * sq1));
        double R1 = (N1 * (1. - LocationConstants.UTM_HOR))
                / (1. - LocationConstants.UTM_HOR * (sq1 * sq1));
        double C1 = LocationConstants.UTM_VER * (cq1 * cq1);
        double T1 = tq1 * tq1;
        double D = (x - LocationConstants.UTMK_WIDTH)
                / (N1 * LocationConstants.UTMK_SCALE);

        double dLat = q1
                - ((N1 * tq1) / R1)
                * ((D * D)
                        / 2.
                        - pow(D, 4.)
                        / 24.
                        * (5. + 3. * T1 + 10. * C1 - 4. * (C1 * C1) - 9. * LocationConstants.UTM_VER) + pow(
                        D, 6.)
                        / 720.
                        * (61. + 90. * T1 + 298. * C1 + 45. * (T1 * T1)
                                - 252. * LocationConstants.UTM_VER - 3. * (C1 * C1)));
        double dLong = LocationConstants.UTMK_LON
                * LocationConstants.RAD
                + (1. / cq1)
                * (D - pow(D, 3.) / 6. * (1. + (2. * T1) + C1) + pow(D, 5.)
                        / 120.
                        * (5. - (2. * C1) + (28. * T1) - (3. * (C1 * C1))
                                + (8. * LocationConstants.UTM_VER) + (24 * (T1 * T1))));

        dLat *= LocationConstants.DEG;
        dLong *= LocationConstants.DEG;

        return new GPSCoordinate(dLong, dLat);
    }
    private static double E1F(double d) {
        return (1.0 - d / 4. - 3. * d * d / 64. - 5. * d * d * d / 256.);
    }
}

Related Tutorials