簡體   English   中英

是否可以在Objective-C中實現Haversine公式並從SQLite中調用它?

[英]Is it possible to implement the Haversine formula in Objective-C and call it from SQLite?

據我所知,SQLite沒有數學函數來直接在SQL中正確實現Haversine公式。 我認為這應該可以使用外部函數 ,實現在C中。

目標是在iPhone中安裝SQLite數據庫,並能夠按距離排序到用戶的當前位置。 我已經搜索過了,但是我找不到這個例子。 我認為困難的部分是讓函數聲明正確。 我希望的最終結果是能夠執行如下的SQL語句:

SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)

我有一個C Haversine公式。 功能定義如下:

float distance( float nLat1, float nLon1, float nLat2, float nLon2 );

有誰知道這是否可能和/或有一些示例代碼從哪里開始?

這演示了一個sqlite函數,它接受一個字符串參數並返回一個字符串結果。

在你的情況,你會需要一個函數讀取四個浮點並返回一個float但原理是一樣的(你將與sqlite3_value_doublesqlite3_result_double sqlite3_result_text替換sqlite3_value_text):

#include <stdlib.h>
#include <sqlite3.h>
#include <stdio.h>


void haver(sqlite3_context* ctx,int cnt,sqlite3_value** val)
{
    printf("In SQLite haver implementation, called for value: %s\n", sqlite3_value_text(*val));

    char * resultOfCall = "Result of function call"; //this would call the distance function
    sqlite3_result_text(ctx, resultOfCall, strlen(resultOfCall), NULL);
}
int cback (void* udata,int ncol,char** value,char** colname)
{
    int i=0;
    for(;i<ncol;i++)
    printf("Result column: %s value: %s   \n", colname[i], value[i]);
    return 0;
}
int main()
{

    sqlite3 * handle;
    int res = sqlite3_open("./test.sql", &handle);

    res = sqlite3_create_function(handle, "haver", 1, SQLITE_UTF8, NULL, &haver, NULL, NULL);

    char * errmsg = NULL;   
    res = sqlite3_exec(handle, "select haver(w) from t", &cback, NULL, &errmsg);
    printf("sqlite3_exec result: %d %s\n", res, errmsg != NULL ? errmsg : "No error");

    sqlite3_close(handle);
}

我對這篇文章感到非常幸運: http//www.thismuchiknow.co.uk/? p = 71

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM