[TIMOB-14343] TiAPI: PlausibleDatabase SQLite Distance function
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | Low |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | iOS |
Labels | ios, sqlite |
Reporter | Ian Tearle |
Assignee | Unknown |
Created | 2013-06-21T10:00:40.000+0000 |
Updated | 2018-02-28T20:03:42.000+0000 |
Description
For the last 4 major versions I have been adding this patch to PlausibleDatabase files, it's about time this was added to the official branch of Titanium.
The following added to the top of "PLSqliteDatabase.m"
#define DEG2RAD(degrees) (degrees * 0.01745327) // degrees * pi over 180
static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
assert(argc == 4);
if (sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL || sqlite3_value_type(argv[2]) == SQLITE_NULL || sqlite3_value_type(argv[3]) == SQLITE_NULL) {
sqlite3_result_null(context);
return;
}
double lat1 = sqlite3_value_double(argv[0]);
double lon1 = sqlite3_value_double(argv[1]);
double lat2 = sqlite3_value_double(argv[2]);
double lon2 = sqlite3_value_double(argv[3]);
double lat1rad = DEG2RAD(lat1);
double lat2rad = DEG2RAD(lat2);
sqlite3_result_double(context, acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(DEG2RAD(lon2) - DEG2RAD(lon1))) * 6378.1);
}
And in the function "openAndReturnError" just before the return:
sqlite3_create_function(_sqlite, "distance", 4, SQLITE_UTF8, NULL, &distanceFunc, NULL, NULL);
Then in the "PLSqliteDatabase.h" file at the very end of the file before the @end:
-(void)distanceFunc:(id)args;
@end
#endif
Attachments
File | Date | Size |
---|---|---|
PLSqliteDatabase.h | 2013-06-21T10:00:40.000+0000 | 2461 |
PLSqliteDatabase.m | 2013-06-21T10:00:40.000+0000 | 16518 |
No comments