开发者

Measuring/Calculating Distance in iOS

开发者 https://www.devze.com 2023-04-12 01:31 出处:网络
I need to calculate the distance between two points in iOS. I can guarantee at least an iPhone 4 so the picture quality on the camera should be good. The id开发者_C百科ea is to calculate the distance

I need to calculate the distance between two points in iOS. I can guarantee at least an iPhone 4 so the picture quality on the camera should be good. The id开发者_C百科ea is to calculate the distance to a point using the picture. There's an app called easyMeasure which does exactly what I need to do.

I'm ok with Pythagoras but this boggles my mind. How would I do something like this?


Ok, so you were correct in that you need to use sine and such. First though, you'll need to find the lens angle of the iPhones camera. Do do this, put the camera a known distance away from the wall and measure how far it is from the edge of the field of vision to the other side and divide by two. To find θ in the picture below, use tanθ = opposite/adjacent, so inverse tan(opposite/adjacent) = θ.

Measuring/Calculating Distance in iOS

Once you know that, you just have the user take a picture, and give a measurement for how big something on the screen really is. Then just use tanθ = opposite/adjacent, and since you now know θ and the opposite distance, adjacent = opposite/tanθ.

Measuring/Calculating Distance in iOS

Hope that helps!


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

精彩评论

暂无评论...
验证码 换一张
取 消