Calculating a radius with longitude and latitude [duplicate]

帅比萌擦擦* 提交于 2021-02-06 09:21:09

问题


I am trying to determine if two locations (each with their own latitude and longitude values) are within a certain distance of each other, a 3 mile radius for example. I have double values to represent the latitude and longitude of each location.

//Location 1
Double lattitude1 = 40.7143528;
Double longitude1 = -74.0059731;

//Location 2
Double lattitude2 = 33.325;
Double longitude2 = 44.422000000000025;

I am wondering how I would determine if these two locations are within each others radius or not because I am not entirely sure how to do this with this type of value.


回答1:


See Great Circle distance

double CoordDistance(double latitude1, double longitude1, double latitude2, double longitude2)
{
    return 6371 * Math.acos(
        Math.sin(latitude1) * Math.sin(latitude2)
        + Math.cos(latitude1) * Math.cos(latitude2) * Math.cos(longitude2 - longitude1));
}

If your latitude and longitude are in degrees convert them to radians.




回答2:


To get the distance between two points you just need to use this snippet of code.

Point2D p1 = new Point2D(lattitude1, longitude2);
Point2D p2 = new Point2D(lattitude2, longitude2);

double distanceBetweenTwoPoints = (double)Math.sqrt((double)Math.pow((p1.x - p2.x), 2.0) + (double)Math.pow((p1.y - p2.y), 2.0));

--- EDITED ---

Please, check comments for a two coordinates (not 2D) solution.



来源:https://stackoverflow.com/questions/15372705/calculating-a-radius-with-longitude-and-latitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!