0

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)

Adam Hoffman
  • 75
  • 1
  • 7
  • Unless your talking about this iBeacon http://en.m.wikipedia.org/wiki/IBeacon , please remove iBeacon from your subject header. – maelswarm Jan 22 '15 at 17:43
  • Isn't this just some rahter basic math question? – Hermann Klecker Jan 22 '15 at 17:46
  • @Stonz2 I have attached the code I am working with, I haven't asked a question before and was sure how to attach the code. – Adam Hoffman Jan 22 '15 at 17:58
  • @HermannKlecker Yes this is a rather basic math question, but I couldn't find a good answer on google, so I turned here for help. – Adam Hoffman Jan 22 '15 at 18:03
  • It is a rather simple trigonometry question, you need to research that. – zaph Jan 22 '15 at 18:11
  • Are you sure the current location is not moving around wildly? Core Location does not work well inside a building for example. And even outside it's not very accurate at short ranges. If you are within range of a bluetooth beacon, then core location is almost useless for determining how close to the beacon you are. GPS is not accurate enough for that. What you can use is the radio signal strength of the beacon, but that doesn't help you with location unless you have several beacons to triangulate against. – Abhi Beckert Jan 23 '15 at 00:17

1 Answers1

1

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:

  1. The location in space from each device which would give you distance between the devices and a direction if you assume the receiving device is always pointed north
  2. Since users often don't face North when using their iPhones, you need to use the compass on the device to figure out how to orient the picture of the arrow pointing on 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.

Community
  • 1
  • 1
Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • I am using iBeacons, so essentially I know my position in the room, imagine the room as a plane of X and Y coords. I want to pick another point in the room at random more or less and then have an arrow on my app point in the direction of that point no matter how much I move around the room. – Adam Hoffman Jan 22 '15 at 22:06
  • is the point at random basically another device that is transmitting? – Josh Gafni Jan 22 '15 at 23:53
  • No the other point is an object in the room is like a desk or a lamp, not transmitting anything. The floor becomes an X and Y grid and I know my position in the room by triangulating iBeacons. I want to point an arrow from my position to the chair or lamp or whatever. this should work even if my position was status like it was also a desk. I guess what I am asking is how do I point from one CGPoint to another CGPoint kinda of like just drawing a line but what I am interested in, is at the end of the line I want an arrow point towards the second object. – Adam Hoffman Jan 23 '15 at 14:20