A possible solution is to use the grid for the entire map you are interested in and pre-assign points to a specific row / column. Then:
- Calculate the grid location of your new point - add a column to it for this.
- Calculate the distance of all coordinates in the current grid - if exists
- You still need to calculate all the distances in the next grid (you are unlikely to be perfectly concentrated in your current square, you always need to check one grid distance from the one in which there was a better match.)
Should greatly reduce the amount of computation you need to do.
If you expect that you will always find a place at a distance of X, you can request the x / y coordinates that fall into this range, your coordinates +/- x KM (square), calculate whether they fall into the xKM circle from your point, and then choose the shortest one.
UPDATE - grid option
I assume that you are already making the distance between the two point calculations and will not describe it.
If you have an atlas, you can see an example by looking at the place in the index. This will give you the page and grid location, for example M5. If you go to this page, it will have rows and columns with numbers and letters, and if you look at the square where lines M and column 5 intersect, you will find a city there. For this you need:
- determine how big your grid is (how dense your points are), it would be nice to have a big grid, and all your points will land one square).
- For each point, calculate which grid it is in. If your polygons are complex, there are many points to copy in the polygon code. If (in my example) you just use squares, you just need to determine which row / column of each point is between them.
- See map for location of user and nearest points:

So, if the user is a green marker, he will be in C4. You would look for all the other points on C4 and determine that the nearest one is No. 2. Then you will also need to check one grid all the way to make sure that the object was not closer than the one you found, so this includes squares: B3, B4, B5, C3, C5, D3, D4, D5. When you do this, you will choose No. 3 with C3 and you will be done.
If the user was in the square D2, where there are no other points, you would find your first match in the word C2. When checking C1, C2, C3, D1, D3, E1, E2, E3. After detection, you will again need to check another radius, which would be the following: B0-4, C0, C4, D0, D4, E0, E4, F0-4. etc. You can see that mesh selection will be important to make it as efficient as possible.
Also note that this assumes your grids are equal in contrast to my drawn example.
Option 2:
If you expect a result in X km and you want your database to calculate quickly, you can do this:
LatMin = currentLatCoord-radiusValInDegrees LatMax = currentLatCoord+radiusValInDegrees LonMin = currentLonCoord-radiusValInDegrees LonMax = currentLonCoord+radiusValInDegrees SELECT * From Locations WHERE Lat BETWEEN LatMin AND LatMax AND Lon BETWEEN LonMin AND LonMax
Now this gives you all the results squared. It is important that you then check that they are actually in the circle - you need to drop any of the corners, since there may actually be closer coordinates than at the edge of the circle. Therefore, for each point, first check to see if it is inside the circle ( Equation for testing if the point is inside the circle ), then calculate the distance and save the nearest one. If you do not get the result, increase the circle.
Again, choosing a good radius will depend on your data.