Search code examples
mathgeolocationgpsgremlingoogle-geolocation

Measure GPS distance between two points without using almost any math? (accuracy is not needed at all)


I'm programming an application where I save GPS location (lat and long) into each user info. I need to search the nearest users, I can only search using a between() function provided by my database, this function only checks if a number is between a range min and max, it's a very limited tool.

Can I use this to find near users?

This is a pseudo-code example of what I could do, this example finds users that their lat and long values are not more or less than 2 comparing with the target user:

lon.is(between(user.lon - 2, user.lon + 2).and(
lat.is(between(user.lat - 2, user.lat + 2))

I don't care about accuracy, I only want to get the closest users in maximum distance of like a city size approximately, that is all I need.

Is this going to work? I'm not familiar with geolocation math and coordinate problems in general.

When I store the GPS data I can convert the data into another format using all the math required without problems, so a coordinte system convertion is available, but for searching I can only use that function.


Solution

  • If you have the differences in latitude and longitude in decimal degrees, you can do a quick calculation for distance.

    Considering the mean Earth radius to be 6,371 km, meaning that each degree covers approximately:

                              (2*pi*6371)/360 = 111.19 Km, 
    

    all you have to do is to take the resultant of the latitude and longitude differences as:

                            sqrt((lat1-lat2)^2 + (lon1-lon2)^2), 
    

    and multiply it for 119.19 to have the distance in Km.

    This is a very rough calculation as you requested, but should be enough if we're talking about city-level precision.

    Update:

    Your pseudocode would look like this:

        #City radius in Km
        city_radius = 60 
    
        #City radius in degrees
        city_radius_degree = 60/111.19
    
        lon.is.NOT(between(user.lon - city_radius_degree, user.lon + city_radius_degree).or(
        lat.is.NOT(between(user.lat - city_radius_degree, user.lat + city_radius_degree))
    

    You're checking if, in any direction, the users are within the same circle. See that I changed the query from and to or, since both don;t have to be true at the same time for the distance to be above a certain threshold.

    Again, this is a very rough approximation, but probably good enough for your intentions.