my iPhone app require me to send the longitude and latitude of the user to a webservice and to extract data (longitude and latitude of a service station) from database in order to calculate the distance between the user and every service station. My question is which is better (side memory and time), calculating the distance directly on the server or on the iPhone application ? thx in advance :)
Asked
Active
Viewed 541 times
3 Answers
2
Let me disclaim that this answer could vary greatly dependent on the case. However, given your brief specification, I would do server-side calculation for the following reasons:
- You are already making a remote call, so the connection overhead exists already
- Processing power (for distance calculations) of the server is far greater than the device
- The increase in response packet size is negligible (barely worth mentioning, but to deter this as a counterpoint)
- Doing so keeps a nice separation of View (the device) and Model (data from remote call)

Jason McCreary
- 71,546
- 23
- 135
- 174
-
Hi, ok i agree that doing that on the server side may be better but i'm afraid there is no functions on php which will do the same work like CLLocationDistance in iOS SDK in order to calculate the distance between two points (longitude and latitude), am i right ?? – Malloc Apr 23 '11 at 21:33
-
1Not at all. I do a very similar thing for my app LastPlayed. I pass the longitude and latitude my API call. See **Black Frogs** answer about how to perform this. But I will say my server side API is on top PHP. So it most definitely can be done. – Jason McCreary Apr 24 '11 at 01:40
2
The phone has a very capable CPU, so why not do it locally? Also, given the recent furor over location tracking, it's probably better not to seem to be uploading tracking data for mysterious purposes.

Allen
- 2,228
- 18
- 22
-
While I agree with the general philosophy, there is nothing mysterious about providing the users location for a location based app. Furthermore most platforms verify the user is wants to allow the app access to location data. – Jason McCreary Apr 24 '11 at 01:43
2
To calculate the distance of two points on Earth, you can use the Haversine formula.
For examples:
- In Perl: http://www2.nau.edu/~cvm/latlon_formula.html
- In Python: http://www.johndcook.com/python_longitude_latitude.html
For PHP, check out this answer: MySQL Great Circle Distance (Haversine formula)

Community
- 1
- 1

Black Frog
- 11,595
- 1
- 35
- 66