You might think of it as a 3D problem. Convert (lat, lon) coordinates to (x, y, z) coordinates. This needs to be done only once for your database.
For each test point, convert to (x, y, z) and calculate the squares of the distance of the chords (for speed and simplicity) for each city. Choose the nearest one. I believe that the nearest city in 3 places will also be the closest city in terms of a large circle.
If you need a long distance, you can only calculate it for the nearest city.
With a (x, y, z) space, perhaps there is a spatial partitioning structure that you can use to limit which cities you should check. I do not think that there is one that will help directly in the (lat, lon) space. But O (N) is really not that bad. In addition, the problem is very well drawn.
source share