Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-14343] TiAPI: PlausibleDatabase SQLite Distance function

GitHub Issuen/a
TypeNew Feature
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labelsios, sqlite
ReporterIan Tearle
AssigneeUnknown
Created2013-06-21T10:00:40.000+0000
Updated2018-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

FileDateSize
PLSqliteDatabase.h2013-06-21T10:00:40.000+00002461
PLSqliteDatabase.m2013-06-21T10:00:40.000+000016518

Comments

No comments

JSON Source