Pen*_*m10 26 sql sqlite android
我有这个查询,它在MySQL中运行良好
SELECT ((ACOS(SIN(12.345 * PI() / 180) * SIN(lat * PI() / 180) +
COS(12.345 * PI() / 180) * COS(lat * PI() / 180) * COS((67.89 - lon) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) AS distance, poi.*
FROM poi
WHERE lang='eng'
HAVING distance<='30'
Run Code Online (Sandbox Code Playgroud)
距离以公里为单位,输入为lat=12.345
和lon=67.89
SQLite是3,我无法在Android上运行自定义函数.我也没有acos()等......因为它不是标准SQLite的一部分.
如何在SQLite中进行上述查询?
Eri*_*rch 31
以下是Java中用于在Android设备上构建基于位置的查询的实现.这个想法来自KennyTM(参见已接受的回复),并暗示在表中添加了4列来存储经度和经度的正弦和余弦值.
以下是在插入时为"Shop"表准备数据的代码:
public static void injectLocationValues(ContentValues values, double latitude, double longitude) {
values.put(LocationColumns.LATITUDE, latitude);
values.put(LocationColumns.LONGITUDE, longitude);
values.put(LocationColumns.COSLAT, Math.cos(MathUtil.deg2rad(latitude)));
values.put(LocationColumns.SINLAT, Math.sin(MathUtil.deg2rad(latitude)));
values.put(LocationColumns.COSLNG, Math.cos(MathUtil.deg2rad(longitude)));
values.put(LocationColumns.SINLNG, Math.sin(MathUtil.deg2rad(longitude)));
}
public static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下函数构建投影:
/**
* Build query based on distance using spherical law of cosinus
*
* d = acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2?long1)).R
* where R=6371 and latitudes and longitudes expressed in radians
*
* In Sqlite we do not have access to acos() sin() and lat() functions.
* Knowing that cos(A-B) = cos(A).cos(B) + sin(A).sin(B)
* We can determine a distance stub as:
* d = sin(lat1).sin(lat2)+cos(lat1).cos(lat2).(cos(long2).cos(long1)+sin(long2).sin(long1))
*
* First comparison point being fixed, sin(lat1) cos(lat1) sin(long1) and cos(long1)
* can be replaced by constants.
*
* Location aware table must therefore have the following columns to build the equation:
* sinlat => sin(radians(lat))
* coslat => cos(radians(lat))
* coslng => cos(radians(lng))
* sinlng => sin(radians(lng))
*
* Function will return a real between -1 and 1 which can be used to order the query.
* Distance in km is after expressed from R.acos(result)
*
* @param latitude, latitude of search
* @param longitude, longitude of search
* @return selection query to compute the distance
*/
public static String buildDistanceQuery(double latitude, double longitude) {
final double coslat = Math.cos(MathUtil.deg2rad(latitude));
final double sinlat = Math.sin(MathUtil.deg2rad(latitude));
final double coslng = Math.cos(MathUtil.deg2rad(longitude));
final double sinlng = Math.sin(MathUtil.deg2rad(longitude));
//@formatter:off
return "(" + coslat + "*" + LocationColumns.COSLAT
+ "*(" + LocationColumns.COSLNG + "*" + coslng
+ "+" + LocationColumns.SINLNG + "*" + sinlng
+ ")+" + sinlat + "*" + LocationColumns.SINLAT
+ ")";
//@formatter:on
}
Run Code Online (Sandbox Code Playgroud)
它将注入一个响应列,其中您需要应用以下公式的距离以公里为单位进行转换:
public static double convertPartialDistanceToKm(double result) {
return Math.acos(result) * 6371;
}
Run Code Online (Sandbox Code Playgroud)
如果要使用部分距离订购查询,则需要订购DESC而不是ASC.
ken*_*ytm 15
您可以创建4个新列,是罪恶和COS lat
和lon
.因为在运行查询之前可以在程序中计算cos(a+b) = cos a cos b - sin a sin b
sin和cos的其他外观SIN(12.345 * PI() / 180)
,所以大的"距离"表达式减少到P * SIN_LAT + Q * COS_LAT + ...
可以由SQLite3处理的形式.
BTW,另请参阅Android上的Sqlite:如何创建一个sqlite dist db函数 - 在app中用于使用lat,long进行距离计算.
scl*_*eys 12
在使用sqlite3 for ios时遇到同样的问题,在使用公式稍微玩一下这是一种方法,不使用sql端的函数(伪代码):
为您存储在数据库中的每个项目预先计算这些值(并存储它们):
cos_lat = cos(lat * PI / 180)
sin_lat = sin(lat * PI / 180)
cos_lng = cos(lng * PI / 180)
sin_lng = sin(lng * PI / 180)
Run Code Online (Sandbox Code Playgroud)在搜索时预先计算这些值(对于给定位置cur_lat,cur_lng)
CUR_cos_lat = cos(cur_lat * PI / 180)
CUR_sin_lat = sin(cur_lat * PI / 180)
CUR_cos_lng = cos(cur_lng * PI / 180)
CUR_sin_lng = sin(cur_lng * PI / 180)
cos_allowed_distance = cos(2.0 / 6371) # This is 2km
Run Code Online (Sandbox Code Playgroud)您的SQL查询将如下所示(将CUR_*替换为您刚刚计算的值)
SELECT*FROM position WHERE CUR_sin_lat*sin_lat + CUR_cos_lat*cos_lat*(cos_lng*CUR_cos_lng + sin_lng*CUR_sin_lng)> cos_allowed_distance;
归档时间: |
|
查看次数: |
22227 次 |
最近记录: |