码迷,mamicode.com
首页 > 其他好文 > 详细

经纬度和距离之间的计算

时间:2017-07-22 15:41:27      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:static   org   port   this   raid   nan   util   travel   dpm   

不是我写的, 也看不太懂, 备忘

package com.iwhere.easy.travel.tool;

import org.junit.Test;
import org.springframework.util.Assert;

/**
 */
public class GeoUtils {

    public final static float EARTH_RADIUS = 6378137; // 地球半径

    @Test
    public void test2() {
//        GeoUtils.getBoxByCoordinate(120.0, 40.0, 2000.0);
        Double distance = GeoUtils.getDistance(new Point(119.9765412051408, 39.98202952055585),
                new Point(120.0234587948592, 40.01797047944415));
        System.out.println(distance);
    }
    
    /**
     * 根据中心点获取box
     * 
     * @return
     */
    public static Double[] getBoxByCoordinate(Double longitude, Double latitude, Double raidusMile) {
        Assert.notNull(longitude);
        Assert.notNull(latitude);

        Double degree = (24901 * 1609) / 360.0; // 获取每度弧长

        Double mpdLng = Double.parseDouble((degree * Math.cos(latitude * (Math.PI / 180)) + "").replace("-", ""));
        Double dpmLng = 1 / mpdLng;
        Double radiusLng = dpmLng * raidusMile;
        // 获取最小经度
        Double minLat = longitude - radiusLng;
        // 获取最大经度
        Double maxLat = longitude + radiusLng;

        Double dpmLat = 1 / degree;
        Double radiusLat = dpmLat * raidusMile;
        // 获取最小纬度
        Double minLng = latitude - radiusLat;
        // 获取最大纬度
        Double maxLng = latitude + radiusLat;
        System.out.println(minLat + ": " + maxLat + ": " + minLng + ": " + maxLng);
        return null;
    }

    /**
     * 获取距离
     * 
     * @param from
     *            开始点
     * @param to
     *            结束点
     * @return
     */
    public static Double getDistance(Point from, Point to) {
        if (from == null || to == null || from.getLat() == null || from.getLng() == null || to.getLat() == null
                || to.getLng() == null) {
            return Double.NaN;
        } else {
            double lat1 = from.getLat() * Math.PI / 180.0;
            double lat2 = to.getLat() * Math.PI / 180.0;
            double a = lat1 - lat2;
            double b = (from.getLng() - to.getLng()) * Math.PI / 180.0;

            double sa2, sb2;
            sa2 = Math.sin(a / 2.0);
            sb2 = Math.sin(b / 2.0);
            return 2 * EARTH_RADIUS * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        }
    }

    /**
     * 
     * ---------------------------------------------------------------------------
     * 点
     * ---------------------------------------------------------------------------
     * <strong>copyright</strong>: ?版权所有 成都都在哪网讯科技有限公司<br>
     * ----------------------------------------------------------------------------
     * 
     * @author: hewei
     * @time:2016年10月28日 下午6:29:22
     *                   ---------------------------------------------------------------------------
     */
    public static class Point {
        public Double lat; // 纬度
        public Double lng; // 经度

        /**
         * 
         */
        public Point() {
            super();
        }

        /**
         * @param lat
         * @param lng
         */
        public Point(Double lat, Double lng) {
            super();
            this.lat = lat;
            this.lng = lng;
        }

        /**
         * @return the lat
         */
        public Double getLat() {
            return lat;
        }

        /**
         * @param lat
         *            the lat to set
         */
        public void setLat(Double lat) {
            this.lat = lat;
        }

        /**
         * @return the lng
         */
        public Double getLng() {
            return lng;
        }

        /**
         * @param lng
         *            the lng to set
         */
        public void setLng(Double lng) {
            this.lng = lng;
        }
    }
}

 

参考: http://blog.csdn.net/koryako/article/details/51864161

经纬度和距离之间的计算

标签:static   org   port   this   raid   nan   util   travel   dpm   

原文地址:http://www.cnblogs.com/wenbronk/p/7221325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!