欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類示例詳解

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

在工作中遇到對經(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)文章

  • springBoot整合redis做緩存具體操作步驟

    springBoot整合redis做緩存具體操作步驟

    緩存主要是將數(shù)據(jù)存在計(jì)算機(jī)的內(nèi)存當(dāng)中,以便于在使用的時(shí)候是可以實(shí)現(xiàn)快速讀取使用,它的快也是相對于硬盤讀取而言,這篇文章主要給大家介紹了關(guān)于springBoot整合redis做緩存的具體操作步驟,需要的朋友可以參考下
    2024-04-04
  • java接口冪等性的實(shí)現(xiàn)方式

    java接口冪等性的實(shí)現(xiàn)方式

    本文介紹了在不同層面上實(shí)現(xiàn)Java接口冪等性的方法,包括使用冪等表、Nginx+Lua和Redis、以及SpringAOP,通過這些方法,可以確保接口在多次請求時(shí)只執(zhí)行一次,避免重復(fù)處理和數(shù)據(jù)不一致,每種方法都有其適用場景和優(yōu)勢,通過實(shí)際測試驗(yàn)證了冪等性邏輯的有效性
    2025-01-01
  • 兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作

    兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作

    這篇文章主要介紹了兩個(gè)小例子輕松搞懂 java 中遞歸與尾遞歸的優(yōu)化操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解

    Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解

    這篇文章主要介紹了Java SpringBoot如何實(shí)現(xiàn)帶界面的代碼生成器,幫助大家更好的理解和使用Java SpringBoot編程語言,感興趣的朋友可以了解下
    2021-09-09
  • JAVA讀取二進(jìn)制文件以及畫圖教程

    JAVA讀取二進(jìn)制文件以及畫圖教程

    由于項(xiàng)目需要,需要對二進(jìn)制文件進(jìn)行讀取,所以這篇文章主要給大家介紹了關(guān)于JAVA讀取二進(jìn)制文件以及畫圖的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • JAVA字符串拼接常見方法匯總

    JAVA字符串拼接常見方法匯總

    這篇文章主要介紹了JAVA字符串拼接常見方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼

    SpringBoot如何使用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-12
  • Java中ScheduledExecutorService的使用方法詳解

    Java中ScheduledExecutorService的使用方法詳解

    ScheduledExecutorService是ExecutorService的一個(gè)子接口,它主要用于在給定的延遲之后或周期性地執(zhí)行任務(wù),本文主要介紹了ScheduledExecutorService的使用方法,感興趣的可以了解下
    2024-12-12
  • java中的過濾器 Filter應(yīng)用小結(jié)

    java中的過濾器 Filter應(yīng)用小結(jié)

    文章主要介紹了Java Web中的過濾器(Filter)的基本概念、生命周期、配置和應(yīng)用,過濾器可以攔截請求和響應(yīng),用于執(zhí)行一些預(yù)處理或后處理操作,如設(shè)置編碼、校驗(yàn)用戶身份等,感興趣的朋友一起看看吧
    2025-03-03
  • Java中靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)和普通代碼塊的區(qū)別

    Java中靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)和普通代碼塊的區(qū)別

    在Java中,靜態(tài)代碼塊、構(gòu)造代碼塊、構(gòu)造函數(shù)、普通代碼塊的執(zhí)行順序是一個(gè)筆試的考點(diǎn),通過這篇文章希望大家能徹底了解它們之間的執(zhí)行順序,需要的朋友可以參考下
    2023-05-05

最新評論