K
K
Kassymbekoff2018-09-29 23:24:30
Yandex maps
Kassymbekoff, 2018-09-29 23:24:30

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

2 answer(s)
B
bask, 2018-09-29
@Kassymbekoff

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!
}

For a circle We measure the distance between two points and compare it with a given radius. If this distance is less than the radius, then the points are adjacent.
Here it is assumed that the distance is small and the calculations take place far from the poles, since there will have to take into account distortion, tk. the square will turn into a parallelogram, and the circle will turn into an ellipse inscribed in such a parallelogram.

M
Moskus, 2018-09-30
@Moskus

Stop doing nonsense and use databases with spatial functions - SpatiaLite/SQLite, PostGIS/PostgreSQL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question