我的Sql Lite数据库中有16000个位置。
我正在取全光标并将每个设置为LOCATION对象,并且我正在使用LOCATION类的distaneTo()方法。
有没有最好的方法来根据用户的位置找到最近的位置。
我的代码::::
Location userLocation = new Location("Point A");
userLocation.setLatitude(latitude);
userLocation.setLongitude(longitude);
Location stopLocation = new Location("Point B");
List<FavouriteStop> allFavourites = new ArrayList<FavouriteStop>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(
"Select * from tbl_stop order by routeName desc", null);
FavouriteStop stop = new FavouriteStop();
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
if (c.getString(c.getColumnIndex("stopId")) != null) {
// Setting Stop Location for Distance
stopLocation.setLatitude(c.getDouble(c
.getColumnIndex("lat")));
stopLocation.setLongitude(c.getDouble(c
.getColumnIndex("lon")));
float distance = userLocation.distanceTo(stopLocation);
if (distance < radius) {
if (allStopsHash.containsKey(c.getString(c
.getColumnIndex("routeName")))) {
// Contains
} else {
// Not Contain
stop.setFirstElement(true);
allStopsHash.put(
c.getString(c.getColumnIndex("routeName")),
c.getString(c.getColumnIndex("routeName")));
}
stop.settag(c.getString(c.getColumnIndex("tag")));
stop.settitle(c.getString(c.getColumnIndex("title")));
stop.setlat(c.getDouble(c.getColumnIndex("lat")));
stop.setlon(c.getDouble(c.getColumnIndex("lon")));
stop.setstopId(c.getString(c.getColumnIndex("stopId")));
stop.setDirection(c.getString(c
.getColumnIndex("towards")));
stop.setRouteName(c.getString(c
.getColumnIndex("routeName")));
stop.setDistance((distance / 1000));
allFavourites.add(stop);
// }
}// Distance Radius Over
}// If StopId != Null Over
} while (c.moveToNext());
}
db.close();
答案 0 :(得分:0)
SQLite无法根据坐标按距离排序。在这种情况下,迭代这些点是正确的解决方案。但是,您可以通过缓存列索引来改进实现。例如,您可以使用类似
的多次调用getColumnIndex(“lat”)int latIndex = getColumnIndex("lat");
并且总是使用latIndex。我从未做过测试来检查性能改进,但这肯定是更好的方法。