Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類示例詳解
更新時間:2023年07月28日 09:29:01 作者:Mcband
這篇文章主要介紹了Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
在工作中遇到對經(jīng)緯度的小數(shù)和度分秒進行互相轉(zhuǎn)換的需求,類似以下:

一.編寫工具類
請求參數(shù)
package com.sinosoft.springbootplus.lft.business.touristres.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
/**
* <pre>
*經(jīng)緯度轉(zhuǎn)換實體
* </pre>
*
* @author mc
* @date 2023-06-13
*/
@Data
@Accessors(chain = true)
@ApiModel(value = "經(jīng)緯度轉(zhuǎn)換實體", description = "經(jīng)緯度轉(zhuǎn)換實體")
public class LatitudeLongitudeConvertDto {
/**
* 類型
*/
@ApiModelProperty(value = "0:度數(shù),1:小數(shù)")
@NotNull(message = "經(jīng)緯度類型不能為空")
private String type;
/**
* 經(jīng)度
*/
@ApiModelProperty(value = "經(jīng)度")
private Double longitude;
/**
* 緯度
*/
@ApiModelProperty(value = "緯度")
private Double latitude ;
/**
* 經(jīng)度-度
*/
@ApiModelProperty(value = "東經(jīng)-度")
private Integer eastMeasure;
/**
* 經(jīng)度-分
*/
@ApiModelProperty(value = "東經(jīng)-分")
private Integer eastDivide;
/**
* 經(jīng)度-秒
*/
@ApiModelProperty(value = "東經(jīng)-秒")
private Double eastSecond;
/**
* 緯度-度
*/
@ApiModelProperty(value = "北緯-度")
private Integer northMeasure ;
/**
* 緯度-分
*/
@ApiModelProperty(value = "北緯-分")
private Double northDivide;
/**
* 緯度-秒
*/
@ApiModelProperty(value = "北緯-秒")
private Double northSecond;
}轉(zhuǎn)換后實體
package com.sinosoft.springbootplus.lft.business.touristres.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
/**
* <pre>
* 經(jīng)緯度轉(zhuǎn)換 viewObject
* </pre>
*
* @author mc
* @date 2023-06-13
*/
@Data
@Accessors(chain = true)
@ApiModel(value = "經(jīng)緯度轉(zhuǎn)換實體", description = "經(jīng)緯度轉(zhuǎn)換實體")
public class LatitudeLongitudeConvertVo {
/**
* 經(jīng)度
*/
@ApiModelProperty(value = "經(jīng)度")
private Double longitude;
/**
* 緯度
*/
@ApiModelProperty(value = "緯度")
private Double latitude ;
/**
* 東經(jīng)-度
*/
@ApiModelProperty(value = "東經(jīng)-度")
private Integer eastMeasure;
/**
* 東經(jīng)-分
*/
@ApiModelProperty(value = "東經(jīng)-分")
private Integer eastDivide;
/**
* 東經(jīng)-秒
*/
@ApiModelProperty(value = "東經(jīng)-秒")
private Double eastSecond;
/**
* 北緯-度
*/
@ApiModelProperty(value = "北緯-度")
private Integer northMeasure ;
/**
* 北緯-分
*/
@ApiModelProperty(value = "北緯-分")
private Integer northDivide;
/**
* 北緯-秒
*/
@ApiModelProperty(value = "北緯-秒")
private Double northSecond;
}service層
/**
* 經(jīng)緯度轉(zhuǎn)換
*/
public LatitudeLongitudeConvertVo latitudeLongitudeConvert(LatitudeLongitudeConvertDto latitudeLongitudeConvertDto) {
LatitudeLongitudeConvertVo latitudeLongitudeConvertVo = LatitudeLongitudeConvert.INSTANCE.latitudeLongitudeConvertDto2LatitudeLongitudeConvertVo(latitudeLongitudeConvertDto);
//小數(shù)->時分秒
if (DECIMAL.equals(latitudeLongitudeConvertDto.getType())) {
//緯度
DegreeMinuteSecondVo latDegreeMinuteSecondVo = LongitudeAndLatitudeUtils.convertToSexagesimal(latitudeLongitudeConvertDto.getLatitude());
//經(jīng)度
DegreeMinuteSecondVo lngDegreeMinuteSecondVo = LongitudeAndLatitudeUtils.convertToSexagesimal(latitudeLongitudeConvertDto.getLongitude());
latitudeLongitudeConvertVo.setEastMeasure(lngDegreeMinuteSecondVo.getMeasure());
latitudeLongitudeConvertVo.setEastDivide(lngDegreeMinuteSecondVo.getDivide());
latitudeLongitudeConvertVo.setEastSecond(lngDegreeMinuteSecondVo.getSecond());
latitudeLongitudeConvertVo.setNorthMeasure(latDegreeMinuteSecondVo.getMeasure());
latitudeLongitudeConvertVo.setNorthDivide(latDegreeMinuteSecondVo.getDivide());
latitudeLongitudeConvertVo.setNorthSecond(latDegreeMinuteSecondVo.getSecond());
}
//時分秒->小數(shù)
if (DEGREES.equals(latitudeLongitudeConvertDto.getType())) {
//經(jīng)度
double lng = LongitudeAndLatitudeUtils.Dms2D(latitudeLongitudeConvertDto.getEastMeasure(), latitudeLongitudeConvertDto.getEastDivide(), latitudeLongitudeConvertDto.getEastSecond());
//緯度
double lat = LongitudeAndLatitudeUtils.Dms2D(latitudeLongitudeConvertDto.getNorthMeasure(), latitudeLongitudeConvertDto.getNorthDivide(), latitudeLongitudeConvertDto.getNorthSecond());
latitudeLongitudeConvertVo.setLatitude(lat);
latitudeLongitudeConvertVo.setLongitude(lng);
}
return latitudeLongitudeConvertVo;
}工具類
package com.sinosoft.springbootplus.lft.business.touristres.utils;
import com.sinosoft.springbootplus.lft.business.touristres.vo.DegreeMinuteSecondVo;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class LongitudeAndLatitudeUtils {
/**
*
* @param du Integer類型
* @param min double類型
* @param sec double類型
* @return double(經(jīng)緯度轉(zhuǎn)換之后的小數(shù))
*/
public static double Dms2D(Integer du,double min,double sec ){
double jwd = 0.00;
String limit = "";
min /= 60;
sec /= 3600;
double xiaoshu = min + sec;
DecimalFormat df = new DecimalFormat("0.000000");
String format = df.format(xiaoshu);
if (format.substring(0, 1).equals("1")) {
du += 1;
limit = String.valueOf(du);
}
String xs = format.substring(1, format.length() - 1);
String stringXs = limit + xs;
jwd = Double.parseDouble(stringXs)+du;
return jwd;
}
/**
* 將小數(shù)度數(shù)轉(zhuǎn)換為度分秒格式
* @param num
* @return
*/
public static DegreeMinuteSecondVo convertToSexagesimal(double num){
DecimalFormat df = new DecimalFormat("0.00");
DegreeMinuteSecondVo degreeMinuteSecondVo = new DegreeMinuteSecondVo();
int du=(int)Math.floor(Math.abs(num)); //獲取整數(shù)部分
double temp=getdPoint(Math.abs(num))*60;
int fen=(int)Math.floor(temp); //獲取整數(shù)部分
double miao=getdPoint(temp)*60;
String format = df.format(miao);
if(num<0){
degreeMinuteSecondVo.setMeasure(-du);
}else{
degreeMinuteSecondVo.setMeasure(du);
}
degreeMinuteSecondVo.setDivide(fen);
degreeMinuteSecondVo.setSecond(Double.parseDouble(format));
return degreeMinuteSecondVo;
}
//獲取小數(shù)部分
private static double getdPoint(double num){
double d = num;
int fInt = (int) d;
BigDecimal b1 = new BigDecimal(Double.toString(d));
BigDecimal b2 = new BigDecimal(Integer.toString(fInt));
double dPoint = b1.subtract(b2).floatValue();
return dPoint;
}
}如果對精度有要求,可以使用以下代碼對精度進行控制
double miao = 1.11111;
//0.00是控制在幾位小數(shù)
DecimalFormat df = new DecimalFormat("0.00");
String format = df.format(miao);到此這篇關(guān)于java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類的文章就介紹到這了,更多相關(guān)java經(jīng)緯度小數(shù)與度分秒轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot使用GuavaCache做簡單緩存處理的方法
這篇文章主要介紹了springboot使用GuavaCache做簡單緩存處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
SpringBoot利用觀察者模式實現(xiàn)聯(lián)動更新機制
觀察者模式(Observer Pattern)是一種軟件設計模式,在許多應用系統(tǒng)中,我們經(jīng)常需要處理多個表之間的關(guān)聯(lián)更新問題,本文將通過一個具體的案例,介紹如何在Spring Boot項目中利用觀察者模式來優(yōu)雅地解決這一需求,需要的朋友可以參考下2024-07-07
SpringBoot實現(xiàn)文件上傳與下載功能的示例代碼
文件上傳與下載是Web應用開發(fā)中常用的功能之一。接下來我們將討論如何在Spring?Boot的Web應用開發(fā)中,如何實現(xiàn)文件的上傳與下載,感興趣的可以了解一下2022-06-06
基于Java快速實現(xiàn)一個簡單版的HashMap詳解
這篇文章主要為大家詳細介紹了如何利用Java簡單實現(xiàn)一個底層數(shù)據(jù)結(jié)構(gòu)為數(shù)組?+?鏈表的HashMap,不考慮鏈表長度超過8個時變?yōu)榧t黑樹的情況,需要的可以參考一下2023-02-02

