Java 地球上计算两点之间距离计算方法


(1)包含经纬度的点对象

package com.ict.dto;

/**
 * Created by chenlong on 2017/7/6.
 * Info:
 */
public class Point {
    public double lon;
    public double lat;

    public Point(double lon, double lat) {
        this.lon = lon;
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public void setLon(double lon) {
        this.lon = lon;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }
}


(2)核心计算方法,返回两个点之间的距离(单位:米)

private int R=6371000;//地球半径
    public double getDistance(Point p1,Point cirp2){
        double rad=Math.PI/180;
        double lat1=p1.getLat()*rad;
        double lat2=cirp2.getLat()*rad;
        double a=Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos((cirp2.getLon()-p1.getLon())*rad);
        double distance=R*Math.acos((Math.min(a,1)));
        return distance;
    }




你可能感兴趣的:(算法和数据结构)