Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類示例詳解
在工作中遇到對經(jīng)緯度的小數(shù)和度分秒進(jìn)行互相轉(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)換實(shí)體 * </pre> * * @author mc * @date 2023-06-13 */ @Data @Accessors(chain = true) @ApiModel(value = "經(jīng)緯度轉(zhuǎn)換實(shí)體", description = "經(jīng)緯度轉(zhuǎn)換實(shí)體") 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)換后實(shí)體
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)換實(shí)體", description = "經(jīng)緯度轉(zhuǎn)換實(shí)體") 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ù)->時(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í)分秒->小數(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; } }
如果對精度有要求,可以使用以下代碼對精度進(jìn)行控制
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)文章
兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作
這篇文章主要介紹了兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解
這篇文章主要介紹了Java SpringBoot如何實(shí)現(xiàn)帶界面的代碼生成器,幫助大家更好的理解和使用Java SpringBoot編程語言,感興趣的朋友可以了解下2021-09-09SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼
本文介紹如何使用SpringBoot、MyBatis-Plus進(jìn)行逆向工程自動(dòng)生成代碼,并結(jié)合Swagger3.0實(shí)現(xiàn)API文檔的自動(dòng)生成和訪問,通過詳細(xì)步驟和配置,確保Swagger與SpringBoot版本兼容,并通過配置文件和測試類實(shí)現(xiàn)代碼生成和Swagger文檔的訪問2024-12-12Java中ScheduledExecutorService的使用方法詳解
ScheduledExecutorService是ExecutorService的一個(gè)子接口,它主要用于在給定的延遲之后或周期性地執(zhí)行任務(wù),本文主要介紹了ScheduledExecutorService的使用方法,感興趣的可以了解下2024-12-12java中的過濾器 Filter應(yīng)用小結(jié)
文章主要介紹了Java Web中的過濾器(Filter)的基本概念、生命周期、配置和應(yīng)用,過濾器可以攔截請求和響應(yīng),用于執(zhí)行一些預(yù)處理或后處理操作,如設(shè)置編碼、校驗(yàn)用戶身份等,感興趣的朋友一起看看吧2025-03-03Java中靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)和普通代碼塊的區(qū)別
在Java中,靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)、普通代碼塊的執(zhí)行順序是一個(gè)筆試的考點(diǎn),通過這篇文章希望大家能徹底了解它們之間的執(zhí)行順序,需要的朋友可以參考下2023-05-05