我假设您经常执行此计算,否则迭代超过一千个点不应该花费大量时间 - 当然不到一秒。
考虑将内存中的点缓存为地理坐标,因为我猜测大部分时间可能用于分配内存和实例化对象,而不是计算距离。然后,您可以从现有的地理坐标列表中对已实例化的现有地理坐标进行计算。
例如:在应用程序加载时,将所有点存储到内存中,可能在后台线程上。
List<GeoCoordinate> points = from point in db.Points select new GeoCoordinate(point.lat, point.lng);
然后,采取你的观点,你试图搜索并循环点
var gcSearch = new GeoCoordinate(coordinates[0], coordinates[1]);
var searchDistance = 1000;
var results = from pSearch in points
where pSearch.GetDistanceTo(gcSearch) < searchDistance
select pSearch;
如果这仍然不够快,请考虑缓存最后搜索的点并在新搜索在相同范围内时返回已知列表。
// in class definition
static GeoCoordinate lastSearchedPoint = null;
static List<GeoCoordinate> lastSearchedResults = null
const searchFudgeDistance = 100;
//in search method
var gcSearch = new GeoCoordinate(coordinates[0], coordinates[1]);
if (lastSearchedPoint != null && gcSearch.GetDistanceTo(lastSearchedPoint) < searchFudgeDistance)
return lastSearchedResults;
lastSearchedPoint = gcSearch;
var searchDistance = 1000;
var results = from pSearch in points
where pSearch.GetDistanceTo(gcSearch) < searchDistance
select pSearch;
//store the results for future searches
lastSearchedResults = results;