求出在某个经纬度方圆多少公里返回的最大最小经纬度

java代码,计算在地图上某个点,以这个点为中心,覆盖若干公里范围的最大和最小经纬度

package test;

public class MainClass {

    public final static class GpsUtil {
        double pi;
        double distance;

        //地球平均半径6371km
        public static final double EARTH_RADIUS = 6371000;

        public GpsUtil() {
            this.pi = Math.PI;//3.141592654;
            //测试好久,发现3535出来的是附近五公里- -!
            distance = 3535;
        }

        /**
         *
         * @param lat 维度
         * @param lon 经度
         * @param distance 多少米范围
         * @return
         */
        public double[] compute(double lat, double lon, double distance) {

            lat = lat * pi / 180;
            lon = lon * pi / 180;  //先换算成弧度
            double rad_dist = distance / EARTH_RADIUS;  //计算X公里在地球圆周上的弧度
            double lat_min = lat - rad_dist;
            double lat_max = lat + rad_dist;   //计算纬度范围
            double lon_min;
            double lon_max;
            //因为纬度在-90度到90度之间,如果超过这个范围,按情况进行赋值
            if (lat_min > -pi / 2 && lat_max < pi / 2) {
                //开始计算经度范围
                double lon_t = Math.asin(Math.sin(rad_dist) / Math.cos(lat));
                lon_min = lon - lon_t;
                //同理,经度的范围在-180度到180度之间
                if (lon_min < -pi)
                    lon_min += 2 * pi;
                lon_max = lon + lon_t;
                if (lon_max > pi)
                    lon_max -= 2 * pi;
            } else {
                lat_min = Math.max(lat_min, -pi / 2);
                lat_max = Math.min(lat_max, pi / 2);
                lon_min = -pi;
                lon_max = pi;
            }
            //最后置换成角度进行输出
            lat_min = lat_min * 180 / pi;
            lat_max = lat_max * 180 / pi;
            lon_min = lon_min * 180 / pi;
            lon_max = lon_max * 180 / pi;
            double result[] = { lat_min, lat_max, lon_min, lon_max };
            return result;
        }

        /**
         * 转化为弧度(rad)
         */
        private double rad(double d) {
            return d * Math.PI / 180.0;
//            return d;
        }

        /**
         * 基于余弦定理求两经纬度距离
         *
         * @param lon1
         *            第一点的精度
         * @param lat1
         *            第一点的纬度
         * @param lon2
         *            第二点的精度
         * @param lat2
         *            第二点的纬度
         * @return 返回的距离,单位m
         */

        public double LantitudeLongitudeDist(double lat1, double lat2, double lon1, double lon2) {
            double radLat1 = rad(lat1);
            double radLat2 = rad(lat2);

            double radLon1 = rad(lon1);
            double radLon2 = rad(lon2);

            if (radLat1 < 0)
                radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
            if (radLat1 > 0)
                radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
            if (radLon1 < 0)
                radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
            if (radLat2 < 0)
                radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
            if (radLat2 > 0)
                radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
            if (radLon2 < 0)
                radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
            double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
            double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
            double z1 = EARTH_RADIUS * Math.cos(radLat1);

            double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
            double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
            double z2 = EARTH_RADIUS * Math.cos(radLat2);

            double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
            //余弦定理求夹角
            double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d)
                    / (2 * EARTH_RADIUS * EARTH_RADIUS));
            double dist = theta * EARTH_RADIUS;
            return dist;
        }

    }

    public static void main(String[] args) {
        GpsUtil gps = new GpsUtil();
        double[] a = gps.compute(30.01254012452224, 121.01244544525456456478797, 3535);
        for (double d : a) {
            System.out.println(d);
        }

        double dis = gps.LantitudeLongitudeDist(a[0], a[1], a[2], a[3]);
        System.out.println("两点之间距离:" + dis + "米");

    }

}

原文地址:https://www.cnblogs.com/bigben0123/p/9663384.html

时间: 2024-11-26 04:42:01

求出在某个经纬度方圆多少公里返回的最大最小经纬度的相关文章

拥有栅格(正方形)经纬度中心点、栅格边长,如何计算最大最小经纬度?

假设我们拥有了一个栅格边长为5米的栅格的中心点经纬度坐标,如何计算栅格的最大最小经纬度呢? 1)首先,需要把经纬度转化为mercator米坐标: 1 class Geometry(_x: Double, _y: Double) { 2 def x: Double = _x 3 4 def y: Double = _y 5 } 6 7 def lonLat2Mercator_(lon: Double, lat: Double): Geometry = { 8 val x = lon * 20037

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

c编程:求出4&amp;#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

//求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max,min; int max1,max2;//记录最大值的坐标 int min1,min2;//记录最小值的坐标 int i,j; int a[4][4]; //为数组赋值 for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%d",&

求出二维数组每一行的平均值

1 //二维数组每行输出平均值 2 //2017.3.7 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 void Rand(int arr[][100], int n); 7 void OutputAvg(int arr[][100], int n); 8 int main() 9 { 10 int arr[100][100]; 11 int n = 10; 12 int sum = 0; 13 //随机初始化数组 14 Rand(a

【c语言】输入一组整数,求出最大子序列的和

// 输入一组整数,求出最大子序列的和. // 例如:序列: - 2 11 - 4 13 - 5 - 2,则最大子序列和为20. // 序列: - 6 2 4 - 7 5 3 2 - 1 6 - 9 10 - 2,则最大子序列和为16 #include <stdio.h> int Max_Son(int *p, int len) { int Max_Sum = 0; int i, j; for (i = 0; i < len; ++i) { int sum = 0; for (j = i

求出数组中所有数字的和&amp;&amp;弹出层效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

欧拉计划013(ProjectEuler013):求出10个数乘积最大的

申明:之前的所有欧拉计划都是用python来写的,的确python来写,代码量极少,学习起来也很方便.但是最近为了找java工作,所以用Java来完成欧拉计划的题,来复习一下Java. Large sum Problem 13 Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339021027987979982208375902465101357402

用js写已知abc+cba = 1333,其中a、b、c均为一位数,编程求出满足条件的a、b、c所有组合。

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 28.0px Consolas; color: #888471 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 28.0px Consolas; color: #888471; min-height: 33.0px } span.Apple-tab-span { white-space: pre } <!--<script type="text/

给定入栈序列,求出合法的出栈序列的个数

思想:1.利用全排列函数next_permutation()求出所有可能的序列 2.从中选出所有正确的序列 #include <iostream> #include <stack> #include <vector> #include <algorithm> using namespace std; //判断序列是否是合法的出栈序列 bool IsPopOrder(const int* pPush,const int* pPop,int n) { if (p