I am working on an iOS app, and I am trying to rotate a UIImageView that has the graphic of an arrow. I have my location in the room as a CGPoint, and I want to show the direction you need to head to reach another CGPoint. How do I generate the angle between the two points, and make sure the arrow is pointed the correct direction, also what is the best way to rotate the UIImageView?
- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}
CGAffineTransform transform = CGAffineTransformMakeRotation([self pointPairToBearingDegrees:yourLocation secondPoint:CGPointMake(0, 0)]);
self.arrow.transform = transform;
The problem is that it just makes the arrow jump wildly instead of pointing to the origin (0,0) from my position (yourLocation.x,yourLocation.y)
It sounds like though you might want to have two devices -- one transmitting, and one receiving, and you want the receiving device to have a compass towards the broadcasting device. This sounds pretty neat.
I would broadcast the longitude and latitude of the broadcasting device as strings through the Major and Minor keys.
Now, you should have two points - the longitude and latitude broadcasting and receiving devices. The receiving device also has a compass.
You can now use the code here: Point to location using compass to determine the direction to point.
Note that in order for this to work, Bluetooth must be turn on for both devices, and the apps must be open on both devices, even on the broadcasting device.
Edit
I assume that the point at random is basically another device that is transmitting. I get that you just want to sense where the signal is coming from, but there is no way for the device to know. Bluetooth is based on radio waves (i.e. light) and there is no way to tell which direction the source is coming from. To make this work you need to have two pieces of information: a location from each device, and a reference point to orient the device:
To figure out the location, you can use GPS, but it needs to be as precise as possible if you're talking about positions with a room. And then you can use the link above to determine the orientation.
Note that there is an Indoor positioning SDK Indoor positioning on iOS with Core Location - not accurate? but it doesn't seem to be as accurate as you would hope.