Answer the question
In order to leave comments, you need to log in
How to determine if the user's coordinates are within a given square (circle)?
Good afternoon. Scenario: User1 marks the latitude and longitude on the Yandex map (the center point is obtained) and sets the radius in meters (separately in the input). User2 sends his geolocation (latitude and longitude).
Question: How to determine if User2 is in a given circle (square) of User2?
Answer the question
In order to leave comments, you need to log in
I wrote about ten years ago, got it out of the closet ...
Option for a square:
double lat1 = 55.77;
double lon1 = 37.77;
double lat2 = 55.66;
double lon2 = 37.66;
double d = 0.300; // - проверяемое расстояние в километрах
double dx1km = 1.0f / (108.307 * Math.Cos(lon1 * 3.14159 / 180.0));// 108.307 - км между 1 градусом по долготе на широте Москвы
double dy1km = 1.0 / 111; // 111 км - расстояние между 1 градусом по широте
double dx = d * dx1km/2;
double dy = d * dy1km;
if (lat2 >= lat1 - dx && lat2 <= lat1 + dx && lon2 >= lon1 - dy && lon2 <= lon1 + dy) {
//we are together!
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question