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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章