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

  • Home
  • SEARCH
  • 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 540925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:17:26+00:00 2026-05-13T10:17:26+00:00

I am using an SQLITE database to store latitudes and longitudes of locations. I

  • 0

I am using an SQLITE database to store latitudes and longitudes of locations.

I want to be able to sort the results by rough distance from the current location. I already have the current location of the device as a double (lat, lng), the lat and lng in the database are also doubles.

What I want is a query that will create a virtual column that I am able to sort the results by.

I currently use a function to show the distance for a selected record:


float pk = (float) (180/3.14159);
float a1 = (float) (db_lat / pk);
float a2 = (float) (db_lon / pk);
float b1 = (float) (current_lat / pk);
float b2 = (float) (current_lon / pk);
float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
double tt = Math.acos(t1 + t2 + t3);
double dist = (6366000*tt);

For example, a MySQL select could be (taken from: http://www.movable-type.co.uk):


Select Lat, Lon, acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))cos(radians(Lon)-$lon))$R As dist
From MyTable
ORDER BY dist DESC

Currently I select locations using the following:


public Cursor locationGetAllRows(long groupid)
{
try
{
return db.query(LOCATION_DATABASE_TABLE, new String[] {
"_id", "lat","lon","groupid"},
"groupid="+groupid, null, null, null, null);
}
catch (SQLException e)
{
Log.e("Exception on query: ", e.toString());
return null;
}
}

OK so is it possible to use the SQLITE database in this way? If not the only option I can think of is to have an extra column, iterate through the rows running the above function on each row and filling out an extra column on the row, then sorting on that column?

  • 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-13T10:17:26+00:00Added an answer on May 13, 2026 at 10:17 am

    This won’t completely help, but for situations like this, seriously consider using rawQuery() instead of query(), so you can pass in a full SQL statement vs. having to chop it into pieces.


    Your bigger problem is that I don’t see that SQLite has trigonometric functions.

    You do not indicate how you are using the Cursor you are getting back from your query. For example, if you are putting the Cursor into some sort of CursorAdapter, you could:

    • convert the Cursor into an ArrayList<Position>, where Position is some Java class you define with your data
    • close the Cursor, to release the RAM it takes up
    • sort the ArrayList<Position> using Arrays.sort()
    • wrap the ArrayList<Position> in an ArrayAdapter<Position> and use that where you had been using your CursorAdapter
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a SQLite database to store values from a data logger. The
I'm using a SQLite database to store cover images of books I have in
I am using an sqlite database to store log data. My table has a
I am using SQLITE database and I store the date as a String. Now
I'm using a SQLite database to store information on players and their teams using
I am using the SQLite database and have the following persistent class (simplified): public
I have a database file that is generated on a PC using Sqlite. This
Using SQLite from .net, Is there a way to access the .dump command or
What's the best way to get the last inserted id using sqlite from Java?
I am using SQLITE database in my iPhone app. I have a refresh button

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.