Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance calculation error #7

Closed
glesage opened this issue May 18, 2013 · 3 comments
Closed

Distance calculation error #7

glesage opened this issue May 18, 2013 · 3 comments
Assignees
Labels

Comments

@glesage
Copy link
Contributor

glesage commented May 18, 2013

Distance calculations for each arobject is incorrect.
For one thing, the formulae:

double latitudeDistance =    max(lat, currentLoc.latitude) - min(lat, currentLoc.latitude);
double longitudeDistance  =  max(lon, currentLoc.longitude) - min(lon, currentLoc.longitude);

return (sqrt(pow(latitudeDistance*lat_over_lon,2) + pow(longitudeDistance, 2))) * meterToMiles;

Is location dependent and invalid.

@ghost ghost assigned glesage May 18, 2013
@glesage
Copy link
Contributor Author

glesage commented May 18, 2013

This is an improvement:

double latitudeDistance =   lat - currentLoc.latitude;
double longitudeDistance  =  lon - currentLoc.longitude;

return (sqrt(pow(latitudeDistance,2) + pow(longitudeDistance*1.3, 2))) * 5;

The "1.3" is how much a degree of lon is 1.3 times less than a lat where I am (here in Chicago)

  1. No need to test the max for lat & lon as they are all squared after anyway so the sign doesn't matter.
  2. It fixes the accuracy problem, but only for me here in Chicago

I might have to implement a lat/lon difference calculator depending on the user's location...

@glesage
Copy link
Contributor Author

glesage commented May 18, 2013

Actually CLLocation has a "distanceFromLocation" callback.. will look into that

http://stackoverflow.com/questions/5198996/cllocation-distancefromlocation

@glesage
Copy link
Contributor Author

glesage commented May 18, 2013

Fixed:

CLLocationCoordinate2D object_loc_coord = CLLocationCoordinate2DMake(lat, lon);

CLLocation *object_location = [[CLLocation alloc] initWithLatitude:object_loc_coord.latitude longitude:object_loc_coord.longitude];
CLLocation *user_location = [[CLLocation alloc] initWithLatitude:user_loc_coord.latitude longitude:user_loc_coord.longitude];

return [object_location distanceFromLocation:user_location];

@glesage glesage closed this as completed May 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant