Distance Calculation With GPS Data
Distance Calculation With GPS Data
Posted by FIZIX Agency on Sun 15th August 2010 4:31pm
We were recently involved with a social app which would allow users to see how far away other people were on their profiles and to search for people who were within certain a distance. So if a user visited someone's profile it would tell them how far away this person was; for example 5 miles away, along with whatever town or city they were in.
It would also allow them to browse people based on distance, so if a user was only interested in finding people within 25 miles they could set their max distance preference to 25 miles, the system would then only list people within that distance, showing the closest first.
The obvious solution to this problem would be to use the devices Location Services api and integrate the app with the Google Maps API; however this wasn't possible; or should we say "allowed" due to Google's policy which states that we need to be displaying the data on a Google Map in order to access the API in this way. So we had to find another API that would allow us to access it in this way without showing a map.
While we found an API and built the solution without any problems, I thought I would share our solution as it could be useful or interesting to other developers.
On the App
Firstly we should describe what happens on the app itself regarding GPS data. Every user has an account, as you expect users accounts are stored in a database on a web server. While a user has the app running the devices Location Services picks up the users current long/lat position. When the position changes significantly; or periodically; the app posts to a secure API on the server which does two things.
1. It logs the current long and lat position for this user
2. It resolves their current town/city from the long/lat position
This data is then stored in the database so that their town is displayed on users profiles and you have the current positioning data for calculations.
When a user views another users profile, the app makes a request to the serverside api for the users profile; when this happens the api makes a request to the web service api to do a distance check, so the distance between user 1 and user 2 is posted back to the app.
When a user is browsing other users, they have a distance selector, the system works in much the same way as above, by calculating the long/lat range to check and only returns other users within range.
Location Services API
We decided to use the GeoNames API as it has an open usage policy, its accurate, includes distance calculation facilities and can resolve town, city and country information from the GPS data.
Getting the real location
I'll start by explaining how to get the users real location from the GPS data using the GeoNames API, a simple process in PHP which involves making a simple call to the GeoNames API; passing the long/lat value and parsing the JSON data that it returns.
So if you were in London, the GPS data would resolve as London (or the specific district), you also have access to other bits of information such as state/county, country and so on.
Grab the resolveCity code from Pastebin
Getting The Distance
The other problem we needed to resolve was how to discover the distance between two people; or more specifically two sets of long/lat data. For this we created a simple function that does the calculation for us, saving api calls.
You simply pass the long/lat data for the two people along with a unit flag (such as M for miles and K for kilimeters); the mathematical function returns the distance between the two sets of long/lat data.
Grab the calculateDistance code from Pastebin
This article has been updated to move source code to our Pastebin account