calculate Longitude Position In Meter - Android Map

Android examples for Map:Latitude Longitude

Description

calculate Longitude Position In Meter

Demo Code


import android.content.Context;

public class Main{
    public static double calcLongitudePositionInMeter(double lng) {
        return calcDistance(0, 0, 0, lng);
    }/*from w w  w  .ja  va 2s . c  o  m*/
    public static double calcDistance(Context context, double lat,
            double lng) {
        double lat2 = restoreLat(context);
        double lng2 = restoreLng(context);
        return calcDistance(lat, lng, lat2, lng2);
    }
    public static double calcDistance(double lat0, double lng0,
            double lat1, double lng1) {
        double R = 6371000;
        double dLat = deg2rad(lat0 - lat1);
        double dLon = deg2rad(lng0 - lng1);
        double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0)
                + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat0))
                * Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return R * c;
    }
    public static double restoreLat(Context context) {
        return PrefUtil.getFloat(context, "last_lat",
                (float) defaultLatitude());
    }
    public static double restoreLng(Context context) {
        return PrefUtil.getFloat(context, "last_lng",
                (float) defaultLongitude());
    }
    public static double deg2rad(double deg) {
        return deg * Math.PI / 180.0;
    }
    public static double defaultLatitude() {
        return 35.681382;
    }
    public static double defaultLongitude() {
        return 139.766084;
    }
}

Related Tutorials