java经纬度小数与度分秒相互转换工具类

在工作中遇到对经纬度的小数和度分秒进行互相转换的需求,类似以下:
在这里插入图片描述

一.编写工具类

请求参数

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>
 *经纬度转换实体
 * </pre>
 *
 * @author mc
 * @date 2023-06-13
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "经纬度转换实体", description = "经纬度转换实体")
public class LatitudeLongitudeConvertDto {
    
    
    /**
     * 类型
     */
    @ApiModelProperty(value = "0:度数,1:小数")
    @NotNull(message = "经纬度类型不能为空")
    private String type;

    /**
     * 经度
     */
    @ApiModelProperty(value = "经度")
    private Double longitude;

    /**
     * 纬度
     */
    @ApiModelProperty(value = "纬度")
    private Double latitude ;

    /**
     * 经度-度
     */
    @ApiModelProperty(value = "东经-度")
    private Integer eastMeasure;

    /**
     * 经度-分
     */
    @ApiModelProperty(value = "东经-分")
    private Integer eastDivide;

    /**
     * 经度-秒
     */
    @ApiModelProperty(value = "东经-秒")
    private Double eastSecond;

    /**
     * 纬度-度
     */
    @ApiModelProperty(value = "北纬-度")
    private Integer northMeasure ;

    /**
     * 纬度-分
     */
    @ApiModelProperty(value = "北纬-分")
    private Double northDivide;

    /**
     * 纬度-秒
     */
    @ApiModelProperty(value = "北纬-秒")
    private Double northSecond;
}

转换后实体

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>
 * 经纬度转换 viewObject
 * </pre>
 *
 * @author mc
 * @date 2023-06-13
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "经纬度转换实体", description = "经纬度转换实体")
public class LatitudeLongitudeConvertVo {
    
    
    /**
     * 经度
     */
    @ApiModelProperty(value = "经度")
    private Double longitude;

    /**
     * 纬度
     */
    @ApiModelProperty(value = "纬度")
    private Double latitude ;

    /**
     * 东经-度
     */
    @ApiModelProperty(value = "东经-度")
    private Integer eastMeasure;

    /**
     * 东经-分
     */
    @ApiModelProperty(value = "东经-分")
    private Integer eastDivide;

    /**
     * 东经-秒
     */
    @ApiModelProperty(value = "东经-秒")
    private Double eastSecond;

    /**
     * 北纬-度
     */
    @ApiModelProperty(value = "北纬-度")
    private Integer northMeasure ;

    /**
     * 北纬-分
     */
    @ApiModelProperty(value = "北纬-分")
    private Integer northDivide;

    /**
     * 北纬-秒
     */
    @ApiModelProperty(value = "北纬-秒")
    private Double northSecond;
}

service层

 /**
     * 经纬度转换
     */
    public LatitudeLongitudeConvertVo latitudeLongitudeConvert(LatitudeLongitudeConvertDto latitudeLongitudeConvertDto) {
    
    
        LatitudeLongitudeConvertVo latitudeLongitudeConvertVo = LatitudeLongitudeConvert.INSTANCE.latitudeLongitudeConvertDto2LatitudeLongitudeConvertVo(latitudeLongitudeConvertDto);
        //小数->时分秒
        if (DECIMAL.equals(latitudeLongitudeConvertDto.getType())) {
    
    
            //纬度
            DegreeMinuteSecondVo latDegreeMinuteSecondVo = LongitudeAndLatitudeUtils.convertToSexagesimal(latitudeLongitudeConvertDto.getLatitude());
            //经度
            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());
        }
        //时分秒->小数
        if (DEGREES.equals(latitudeLongitudeConvertDto.getType())) {
    
    
            //经度
            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(经纬度转换之后的小数)
     */
    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;
    }

    /**
     * 将小数度数转换为度分秒格式
     * @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));    //获取整数部分
        double temp=getdPoint(Math.abs(num))*60;
        int fen=(int)Math.floor(temp); //获取整数部分
        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;

    }
    //获取小数部分
    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是控制在几位小数
DecimalFormat df = new DecimalFormat("0.00");
String format = df.format(miao);

猜你喜欢

转载自blog.csdn.net/mcband/article/details/131250787