# IOS simple imitation casually walk AR function (principle)

The final effect of the first dynamic diagram,

~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~

1, first we want to customize a camera interface, you can use AVCaptureSession from the definition, do not need other buttons, only a preview of the interface;

2, we need to draw a simple radar map, you can use CGContextRef to achieve a simple, radar diagram is used to show you and your users in the vicinity (object) distance, with the actual object of latitude and longitude to calculate the distance between two points, by a certain proportion of mapping to the radar map, the distance between two points the calculation formula is as follows:

/ / calculate the distance of two points of latitude and longitude

– (float) DistanceFromCoordinates: (CLLocationCoordinate2D) myDotother: (CLLocationCoordinate2D) otherDot

{

Doubleb = (myDot.longitude- otherDot.longitude) /180.0 * M_PI;

Doubles =22* asin (sqrt (pow (sin (a /2), 2)

* pow (sin (B /2), 2)));

S = round (s*10000) /10000;

Returns;

}

Is the object in the radar map on the display position according to the trigonometric function, on the edge of sinA= / cosA= / hypotenuse, adjacent side bevel, bevel we have, is the distance between two points, then we need to know a point of view, to get an edge through this edge radius with addition and subtraction, it you can calculate the object position in the radar on the map. So we have to calculate the azimuth of two points, look at the following picture:

An explanation of the azimuth of the wiki, we can use the tan2 function to calculate, the formula is as follows:

– (float) getHeadingForDirectionFromCoordinate: (CLLocationCoordinate2D) fromLoctoCoordinate: (CLLocationCoordinate2D) toLoc

{

Floatdegree = RadiansToDegrees (atan2 (sin (tLng-fLng) *cos (tLat), cos (fLat) *sin (tLat) -sin (fLat) *cos (tLat) *cos (tLng-fLng)));

If (degree > =0) {

Returndegree;

}else{

Return (360+degree);

}

}

3, to realize the radar map rotation and rotation follow the mobile phone, here we will use the compass principle, through the CLLocationManager management class, there is a CLHeading class, we can see the compass, the structure of this class:

There are north, North and magnetic values in X, y, Z three axis magnetic value, but when I use the three values when problems are found, when before and after the mobile phone, found that this value changes have stopped, if come to realize that moves is not smooth with this value, so I also use the gyroscope data, using the CMMotionManager management class to get the mobile phone swing angle, when used for mobile computing and mobile phone, mobile phone interface on the position of the object in the.

– (void) startMotion

{

If ([_mgrisDeviceMotionActive]! & & [_mgrisDeviceMotionAvailable])

{

/ / set the sampling interval

_mgr.deviceMotionUpdateInterval=0.1;

NSOperationQueue*queue = [[NSOperationQueuealloc]init];

WithHandler:^ (CMDeviceMotion* _Nullable motion,

NSError* _Nullable error) {

DoublegravityX = motion.gravity.x;

DoublegravityY = motion.gravity.y;

DoublegravityZ = motion.gravity.z;

If (gravityY< =0& & gravityY> =-1)

{

The mobile phone gets / / tilt angle (zTheta angle, and the horizontal plane is mobile phone xyTheta mobile phone around its own rotation angle):

ZTheta (gravityZ, atan2 = sqrtf (gravityX*gravityX+gravityY*gravityY)) /M_PI*180.0;

}

[selfupdataPoint];

}];