Measure / calculate distance in iOS

I need to calculate the distance between two points in iOS. I can guarantee at least the iPhone 4, so the image quality on the camera should be good. The idea is to calculate the distance to a point using an image. There's an app called easyMeasure that does exactly what I need to do.

I'm fine with Pythagoras, but it scares my mind. How do I do something like this?

+4
source share
2 answers

Well, that’s why you were right that you needed to use sine and the like. First, you will need to find the viewing angle of the iPhone camera. Do this, place the camera at a known distance from the wall and measure how far it is from the edge of the field of view to the other side and divide it into two parts. To find θ in the figure below, use tanθ = opposite / adjacent, so the inverse tan (opposite / adjacent) = θ.

Picture 2

Once you know this, you just ask the user to take a picture and give an estimate of how big something is actually on the screen. Then just use tanθ = opposite / adjacent, and since you now know θ and the opposite distance, adjacent = opposite / tan θ.

Picture 1

Hope this helps!

+4
source

New update in ios7
@import CoreLocation; @import MapKit;

CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333]; CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675]; CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco]; MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init]; formatter.units = MKDistanceFormatterUnitsImperial; NSLog(@"%@", [formatter stringFromDistance:distance]); // 535 miles 
0
source

All Articles