Calculation of radius with longitude and latitude

I am trying to determine if two locations (each with its own latitude and longitude values) are at a certain distance from each other, for example, with a radius of 3 miles. I have double meanings 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 in each other's radius or not, because I'm not quite sure how to do this with this type of value.

+4
source share
2 answers

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 in degrees will convert them to radians.

+2
source

To get the distance between two points, you just need to use this piece 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 two coordinates (not 2D).

0
source

Source: https://habr.com/ru/post/1468838/


All Articles