首先感谢维基百科,再者感谢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上的解决方案其实也是可以做到的,大家也可以参考下: