Java经纬度小数与度分秒相互转换工具类示例详解

 更新时间:2023年07月28日 09:29:01   作者:Mcband  
这篇文章主要介绍了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);

到此这篇关于java经纬度小数与度分秒相互转换工具类的文章就介绍到这了,更多相关java经纬度小数与度分秒转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot使用GuavaCache做简单缓存处理的方法

    springboot使用GuavaCache做简单缓存处理的方法

    这篇文章主要介绍了springboot使用GuavaCache做简单缓存处理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • SpringBoot利用观察者模式实现联动更新机制

    SpringBoot利用观察者模式实现联动更新机制

    观察者模式(Observer Pattern)是一种软件设计模式,在许多应用系统中,我们经常需要处理多个表之间的关联更新问题,本文将通过一个具体的案例,介绍如何在Spring Boot项目中利用观察者模式来优雅地解决这一需求,需要的朋友可以参考下
    2024-07-07
  • SpringBoot实现文件上传与下载功能的示例代码

    SpringBoot实现文件上传与下载功能的示例代码

    文件上传与下载是Web应用开发中常用的功能之一。接下来我们将讨论如何在Spring Boot的Web应用开发中,如何实现文件的上传与下载,感兴趣的可以了解一下
    2022-06-06
  • Java中创建线程的四种方式的最佳实践

    Java中创建线程的四种方式的最佳实践

    这篇文章主要为大家详细介绍了Java中创建线程的四种方式的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-08-08
  • 基于Java快速实现一个简单版的HashMap详解

    基于Java快速实现一个简单版的HashMap详解

    这篇文章主要为大家详细介绍了如何利用Java简单实现一个底层数据结构为数组 + 链表的HashMap,不考虑链表长度超过8个时变为红黑树的情况,需要的可以参考一下
    2023-02-02
  • java中的正则操作方法总结

    java中的正则操作方法总结

    关于正则表达式的使用,更多的是自己的经验,有兴趣可以参阅相关书籍。这里主要写一下java中的正则操作方法
    2013-10-10
  • SpringBoot 调度任务及常用任务表达式

    SpringBoot 调度任务及常用任务表达式

    这篇文章主要介绍了SpringBoot 调度任务及常用任务表达式,需要的朋友可以参考下
    2017-12-12
  • SpringBoot中定时任务的使用方法解析

    SpringBoot中定时任务的使用方法解析

    这篇文章主要介绍了SpringBoot中定时任务的使用方法解析,@EnableScheduling 注解,它的作用是发现注解 @Scheduled的任务并由后台执行,没有它的话将无法执行定时任务,需要的朋友可以参考下
    2024-01-01
  • java读取ftp中TXT文件的案例

    java读取ftp中TXT文件的案例

    这篇文章主要介绍了java读取ftp中TXT文件的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 详解Java的Proxy动态代理机制

    详解Java的Proxy动态代理机制

    Java有两种代理方式,一种是静态代理,另一种是动态代理。对于静态代理,其实就是通过依赖注入,对对象进行封装,不让外部知道实现的细节。很多 API 就是通过这种形式来封装的
    2021-06-06

最新评论