如何将地图SDK中获取到的经纬度信息写入到exif中

本文介绍了如何将通过地图SDK获取的经纬度信息转换为DMS(度分秒)格式,以便符合Exif标准进行存储。转换过程中涉及到对半球信息的表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先感谢维基百科,再者感谢stackoverflow

援引自这里,可以将经纬度信息转成DMS格式的这个是exif中存储的格式

http://en.wikipedia.org/wiki/Geographic_coordinate_conversion#Components_of_a_typical_coordinate


java代码如下:

String decimalToDMS(double coord) {
	String output, degrees, minutes, seconds;
 
	// gets the modulus the coordinate divided by one (MOD1).
	// in other words gets all the numbers after the decimal point.
	// e.g. mod := -79.982195 % 1 == 0.982195 
	//
	// next get the integer part of the coord. On other words the whole number part.
	// e.g. intPart := -79
 
	double mod = coord % 1;
	int intPart = (int)coord;
 
	//set degrees to the value of intPart
	//e.g. degrees := "-79"
 
	degrees = String.valueOf(intPart);
 
	// next times the MOD1 of degrees by 60 so we can find the integer part for minutes.
	// get the MOD1 of the new coord to find the numbers after the decimal point.
	// e.g. coord :=  0.982195 * 60 == 58.9317
	//	mod   := 58.9317 % 1    == 0.9317
	//
	// next get the value of the integer part of the coord.
	// e.g. intPart := 58
 
	coord = mod * 60;
	mod = coord % 1;
	intPart = (int)coord;
        if (intPart < 0) {
           // Convert number to positive if it's negative.
           intPart *= -1;
        }
 
	// set minutes to the value of intPart.
	// e.g. minutes = "58"
	minutes = String.valueOf(intPart);
 
	//do the same again for minutes
	//e.g. coord := 0.9317 * 60 == 55.902
	//e.g. intPart := 55
	coord = mod * 60;
	intPart = (int)coord;
        if (intPart < 0) {
           // Convert number to positive if it's negative.
           intPart *= -1;
        }
 
	// set seconds to the value of intPart.
	// e.g. seconds = "55"
	seconds = String.valueOf(intPart);
 
	// I used this format for android but you can change it 
	// to return in whatever format you like
	// e.g. output = "-79/1,58/1,56/1"
	output = degrees + "/1," + minutes + "/1," + seconds + "/1";
 
	//Standard output of D°M′S″
	//output = degrees + "°" + minutes + "'" + seconds + "\"";
 
	return output;
}

转成DMS后还需要表示经纬度所在的半球等信息

  exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, lon>0?"E":"W");
  exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, lat>0?"N":"S");
全部代码如下:
 try {
            ExifInterface exif = new ExifInterface(path);
            final String exifLatitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            
            if (TextUtils.isEmpty(exifLatitude)) {// 只有当位置信息为空的时候才写入
              exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, decimalToDMS(latitude));
              exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, latitude > 0 ? "N" : "S");
              exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, decimalToDMS(longitude));
              exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, longitude > 0 ? "E" : "W");
              exif.saveAttributes();
            }
          } catch (IOException e) {
            LogUtil.e(TAG, "", e);
          }

 private String decimalToDMS(double coord) {
    String output, degrees, minutes, seconds;


    // gets the modulus the coordinate divided by one (MOD1).
    // in other words gets all the numbers after the decimal point.
    // e.g. mod := -79.982195 % 1 == 0.982195
    //
    // next get the integer part of the coord. On other words the whole number
    // part.
    // e.g. intPart := -79


    double mod = coord % 1;
    int intPart = (int) coord;


    // set degrees to the value of intPart
    // e.g. degrees := "-79"


    degrees = String.valueOf(intPart);


    // next times the MOD1 of degrees by 60 so we can find the integer part for
    // minutes.
    // get the MOD1 of the new coord to find the numbers after the decimal
    // point.
    // e.g. coord := 0.982195 * 60 == 58.9317
    // mod := 58.9317 % 1 == 0.9317
    //
    // next get the value of the integer part of the coord.
    // e.g. intPart := 58


    coord = mod * 60;
    mod = coord % 1;
    intPart = (int) coord;
    if (intPart < 0) {
      // Convert number to positive if it's negative.
      intPart *= -1;
    }


    // set minutes to the value of intPart.
    // e.g. minutes = "58"
    minutes = String.valueOf(intPart);


    // do the same again for minutes
    // e.g. coord := 0.9317 * 60 == 55.902
    // e.g. intPart := 55
    coord = mod * 60;
    intPart = (int) coord;
    if (intPart < 0) {
      // Convert number to positive if it's negative.
      intPart *= -1;
    }


    // set seconds to the value of intPart.
    // e.g. seconds = "55"
    seconds = String.valueOf(intPart);


    // I used this format for android but you can change it
    // to return in whatever format you like
    // e.g. output = "-79/1,58/1,56/1"
    output = degrees + "/1," + minutes + "/1," + seconds + "/1";


    // Standard output of D°M′S″
    // output = degrees + "°" + minutes + "'" + seconds + "\"";


    return output;
}
最后stack上的解决方案其实也是可以做到的,大家也可以参考下:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值