Java GPS 经纬度 相关操作

  1. GPS坐标(经纬度)转化为地理位置(省市区)
  2. 两个位置(经纬度)之间的距离,精确到米
  3. 需要导入GsonJsonParser相关的jar包或者maven依赖
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

import static java.lang.Math.*;
import static java.lang.Math.PI;

//经纬度转化为省市区
@Component
public class LocationUtil {

    private static Logger logger = LoggerFactory.getLogger(LocationUtil.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value("${com.address.analyze.urlString}")
    private String urlString;

    @Value("${com.address.analyze.user}")
    private String urlUser;

    /**
     * 通过经纬度信息得到地理位置信息
     */
    public String getAddress(String latitude, String longitude) throws RuntimeException{
        //latitude  纬度 
        //longitude  经度 
        //http://116.196.105.215:1234/gis?auth_user=freevip&latitude=39.880655&longitude=116.354386
        if (StringUtils.isBlank(longitude) || StringUtils.isBlank(latitude)) {
            logger.debug("longitude or longitude is blank... longitude : {} , longitude : {}.", longitude, latitude);
            throw new RuntimeException("fail.logOrLat.blank");
        }
        String url = urlString.replace("{auth_user}", urlUser)
                .replace("{longitude}", longitude)
                .replace("{latitude}", latitude);

        String addressInfoString = restTemplate.getForObject(url, String.class);
        Map<String, Object> addressInfo = new GsonJsonParser().parseMap(addressInfoString);
        return analyzeAddressInfo(addressInfo);
    }

    /**
     * 解析结果,得到省市区
     */
    private String analyzeAddressInfo(Map<String, Object> addressInfo) {
        Map<String, Object> locationMap = (Map<String, Object>) addressInfo.get("data");
        if (locationMap == null || locationMap.isEmpty()) {
            return "";
        }
        StringBuffer result = new StringBuffer();
        for (Map.Entry<String, Object> entry : locationMap.entrySet()) {
            if (entry.getKey().contains("zh")) {
                result.append(entry.getValue());
            }
        }
        return result.toString();
    }
		
	//高德地图可以做验证
    //https://lbs.amap.com/api/javascript-api/example/calcutation/calculate-distance-between-two-markers
    //https://lbs.amap.com/console/show/picker
    /**
     * 地球半径
     */
    private static double EARTH_RADIUS = 6378137;

    /**
     * 两个经纬度之间的直线距离,精确到米
     */
    public double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = Rad(lat1);
        double radLng1 = Rad(lng1);
        double radLat2 = Rad(lat2);
        double radLng2 = Rad(lng2);
        double a = radLat1 - radLat2;
        double b = radLng1 - radLng2;
        double result = 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2))) * EARTH_RADIUS;
        return result;
    }

    /**
     * 经纬度转化成弧度
     */
    private double Rad(double d) {
        return d * PI / 180d;
    }
}

配置

#经纬度转化为省市区
com.address.analyze.urlString=http://116.196.105.215:1234/gis?auth_user={auth_user}&latitude={latitude}&longitude={longitude}
com.address.analyze.user=freevip
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

God__is__a__girl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值