DoubleRadiansDoubledegrees) { returnDegrees * M_PI/180.0;}- (void) testdistance{fmdatabase*db =[Fmdatabase Databasewithpath:_writabledbpath]; if(![DB Open]) { return; } [db makefunctionnamed:@"Distance"Maximumarguments:4withblock:^ (Sqlite3_context *context,intARGC, Sqlite3_value * *argv) { Doublevalues[4]; //get the double values for the four arguments for(inti =0; I <4; i++) { intDataType =Sqlite3_value_numeric_type (Argv[i]); if(DataType = = Sqlite_integer | | dataType = =sqlite_float) {Values[i]=sqlite3_value_double (Argv[i]); } Else{sqlite3_result_null (context); return; } } //Let ' s give those values meaningful variable names Doublelat = radians (values[0]); DoubleLNG = radians (values[1]); DoubleLAT2 = radians (values[2]); DoubleLng2 = radians (values[3]); //Calculate the distance Doubleresult =6371.393* ACOs (cos (LAT2) * cos (LAT) * cos (LNG2-LNG) + sin (lat2) *sin (lat)); Sqlite3_result_double (context, result); }]; intRowCount =0; Fmresultset*rs = [db executeQuery:@"SELECT *,distance (lat,lng,34.27344,108.95996) as distance from hotel WHERE Cityid = ten and Lat > 33.27344 and Lat & Lt 35.27344 and LNG > 107.95996 and LNG < 109.95996 ORDER by distance ASC LIMIT"]; while([Rs next]) {RowCount++; NSLog (@"Does%@,%@", [Rs Stringforcolumnindex:1],[rs Stringforcolumn:@"Distance"]); }}
Query in database, distance code between two latitude and longitude