promet / PRAugmentedReality

iOS Augmented Reality Framework
http://praugmentedreality.com/
MIT License
606 stars 162 forks source link

Distance calculation error #7

Closed glesage closed 11 years ago

glesage commented 11 years ago

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.

glesage commented 11 years ago

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 commented 11 years ago

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

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

glesage commented 11 years ago

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];