which is better, calculating the distance between 2 points on the server or on the iOS application?
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 :)
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)
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.
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)
精彩评论