B4J Question GPS position archive procedure and search for the 'nearest' points

amorosik

Expert
Licensed User
I'm trying to understand how to proceed to create an archive of information relating to scooters located in the city
In addition to the factory and model and serial number there will also be fields for the current geographical position, essentially two fields X=latitude and Y=longitude
I would then like to perform the search to show on the video the 10 scooters closest to the
How to select the 10 scooters 'closest' to a certain position among the 1000 available?
 

Daestrum

Expert
Licensed User
Longtime User
copilot said this - where mx,my is your location and x,y is the scooters location
B4X:
SELECT
    location_id,
    x,
    y,
    -- Calculate distance using Haversine formula
    6371 * ACOS(
        COS(RADIANS(mx)) * COS(RADIANS(x)) * COS(RADIANS(y) - RADIANS(my)) +
        SIN(RADIANS(mx)) * SIN(RADIANS(x))
    ) AS distance
FROM
    your_table
ORDER BY
    distance
LIMIT 10;
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
copilot said this - where mx,my is your location and x,y is the scooters location
B4X:
SELECT
    location_id,
    x,
    y,
    -- Calculate distance using Haversine formula
    6371 * ACOS(
        COS(RADIANS(mx)) * COS(RADIANS(x)) * COS(RADIANS(y) - RADIANS(my)) +
        SIN(RADIANS(mx)) * SIN(RADIANS(x))
    ) AS distance
FROM
    your_table
ORDER BY
    distance
LIMIT 10;
Nice but copilot is over complicating it.

For comparing small distances just use Pythagoras to calculate a distance between two x/y pairs. It may not be as accurate but it’s good enough for sorting points to find the closest 10 etc.

I know this from when I used to write software that warned of speeding cameras. The software had a radar display of the closest cameras.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see
 
Upvote 0
Top