I have this table in sqlite
Locations
ID
Lat ( latitude)
Lon ( longitude)
Type
Name
City
I have for example 100 records what I need is to
Let me make sure this is right: You have point a, and a table of points a[]. Currently, you do something like:
- loop over
b[]
- get
distancefromb[i]toa- if
distanceis less thanminimumDistance
- set
minimumDistance = distance- set
closestPoint = i- return
closestPoint
If so, you're finding it in O(n) time, which can't really be improved upon much. You have to check all the points to see which are the closest.
However, as Matthew points out, you can prune n by assigning points to a grid. This can drastically reduce the amount of points needed to compare. The expense is a bit of time preprocessing, and slightly more complicated logic. If you have a long list of points(or the distance() function takes a long time), you'll definitely want to do something like this.