安卓图片Exif中存储的经纬度的转换

来源:互联网 发布:美国博士申请条件 知乎 编辑:程序博客网 时间:2024/05/21 11:17

1、 了解系统相机中 安卓图片Exif中存储的经纬度 的格式 , exif中GPS格式 是 DMS格式

取值方式如下:

String lon = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String lat = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);

结果如下:  这种度分秒格式的 经纬度  称为 DMS 格式, D(degree)为度、M(minute)为分、S(second)

lon = 114/1,23/1,538331/10000

lat = 30/1,28/1,434171/10000



DMS 与我们 通常使用的 百度地图定位 里 得到  double类型的 经纬度 如何 转换呢!


DMS 转换成 DD格式:

/** * 度分秒 格式 的经纬度 转换成 double类型的经纬度 * Created by meikai on 2017/09/19. */public class LatLonRational2FloatConverter {    /**     * @param rationalString 度分秒格式的经纬度字符串,形如: 114/1,23/1,538547/10000 或 30/1,28/1,432120/10000     * @param ref 东西经 或 南北纬 的标记 S南纬 W西经     * @return double格式的 经纬度     */    public float convertRationalLatLonToFloat(String rationalString, String ref) {        if (StringUtils.isEmpty(rationalString) || StringUtils.isEmpty(ref)) {            return 0;        }        try {            String[] parts = rationalString.split(",");            String[] pair;            pair = parts[0].split("/");            double degrees = parseDouble(pair[0].trim(), 0)                    / parseDouble(pair[1].trim(), 1);            pair = parts[1].split("/");            double minutes = parseDouble(pair[0].trim(), 0)                    / parseDouble(pair[1].trim(), 1);            pair = parts[2].split("/");            double seconds = parseDouble(pair[0].trim(), 0)                    / parseDouble(pair[1].trim(), 1);            double result = degrees + (minutes / 60.0) + (seconds / 3600.0);            if (("S".equals(ref) || "W".equals(ref))) {                return (float) -result;            }            return (float) result;        } catch (NumberFormatException e) {            return 0;        } catch (ArrayIndexOutOfBoundsException e) {            return 0;        } catch (Throwable e) {            return 0;        }    }    private static double parseDouble(String doubleValue, double defaultValue) {        try {            return Double.parseDouble(doubleValue);        } catch (Throwable t) {            return defaultValue;        }    }}


DD格式转换成 DMS格式:


/** * 将gps的经纬度变成度分秒 */public static String degressToString(double digitalDegree) {    double num = 60;    int degree = (int) digitalDegree;    double tmp = (digitalDegree - degree) * num;    int minute = (int) tmp;    int second = (int) (10000 * (tmp - minute) * num);    return degree + "/1," + minute + "/1," + second + "/10000";}


原创粉丝点击