Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:36:54+00:00 2026-05-16T04:36:54+00:00

Given a database that contains three fields: Latitude Longitude Proximity Where Lat and Long

  • 0

Given a database that contains three fields:

Latitude
Longitude
Proximity

Where Lat and Long are GPS coordinates, and Proximity is (some unit – feet? Seconds? Minutes?)

And given the user’s current GPS lat/long…

I want to write a SQL query that will retrieve all rows where the user is within “Proximity” of those rows.

And the trick: This has to work in SQLite, which only supports fairly primitive data types. No cheating and relying on SQL Server (or some other product that provides better geospace functions).

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:36:55+00:00Added an answer on May 16, 2026 at 4:36 am

    Here is a C-based custom function for sqlite [copied from links noted below]. This can be used within an iOS app. It assumes you have columns named latitude and longitude, and calculates the difference between them and any lat/long coordinates you provide. Excellent write-up, works as-is.

    #define DEG2RAD(degrees) (degrees * 0.01745327) // degrees * pi over 180
    
    static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
    {
      // check that we have four arguments (lat1, lon1, lat2, lon2)
      assert(argc == 4);
      // check that all four arguments are non-null
      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;
    }
    
    // get the four argument values
    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]);
    
    // convert lat1 and lat2 into radians now, to avoid doing it twice below
    double lat1rad = DEG2RAD(lat1);
    double lat2rad = DEG2RAD(lat2);
    
    // apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately
    
    // 6378.1 is the approximate radius of the earth in kilometres
    sqlite3_result_double(context, acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(DEG2RAD(lon2) - DEG2RAD(lon1))) *
    6378.1);
    }
    

    This defines an SQL function distance(Latitude1, Longitude1,
    Latitude2, Longitude2), which returns the distance (in kilometres)
    between two points.

    To use this function, add the code above … and
    then add this line immediately after you call sqlite3_open:

    sqlite3_create_function(sqliteDatabasePtr, "distance", 4, SQLITE_UTF8, NULL, &distanceFunc, NULL, NULL);
    

    …where sqliteDatabasePtr is the database pointer returned by your call
    to sqlite3_open.

    Assuming you have a table called Locations, with columns called
    Latitude and Longitude (both of type double) containing values in
    degrees, you can then use this function in your SQL like this:

    SELECT * FROM Locations ORDER BY distance(Latitude, Longitude, 51.503357, -0.1199)
    

    This example orders the locations in your database based on how far
    away they are from the London Eye, which is at 51.503357, -0.1199.

    EDIT :

    Original link http://www.thismuchiknow.co.uk/?p=71 is dead, so as someone mentioned in comment, you can use this link : https://web.archive.org/web/20160808122817/http://www.thismuchiknow.co.uk/?p=71 to get that webpage

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was given a MySQL database file that I need to restore as a
We have a whole bunch of DLLs that give us access to our database
Given a database field named widget_ids, containing data like 67/797/124/ or 45/, where the
Given a table named person (in a MySQL database/schema), kind of like this one:
For a given form in a database, is there a quick/easy way to find
I was given a task to display when a record in the database was
Given a model's instance object, how can I get the database table's name? I
Given the following XML: <databases> <database> <title_display>Aardvark</title_display> </database> <database> <title_display>Apple</title_display> </database> <database> <title_display>Blue</title_display> </database>
I need to test whether various types of database objects exist in a given
Does anyone know a way to auto-generate database tables for a given class? I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.