views:

63

answers:

1

Hi everyone, I'm doing an AR app that shows a UILabel in the cameraView depending of the position of a concrete place. When the compass of the iphone is leading to the place, the label is shown in the center of the screen.
My problem is that I want this label rotates to the left or the right when I turn the iphone. I guess there is some way to map the degrees of the compass to the screen coordinates so I could redraw the label in the correct position of the screen, but I cannot imagine how to do this. I don't need the accelerometer to do this, I only need transforming the compass values to the screen coordinates. Anybody knows some solution? Thank you!

+1  A: 

Assuming your current position and the destination position are expressed in CLLocationCoordinate2D - use the following to get the angle (in radians) from your source, to the destination:

- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc

{

    float fLat = [self angleToRadians:fromLoc.latitude];
    float fLng = [self angleToRadians:fromLoc.longitude];
    float tLat = [self angleToRadians:toLoc.latitude];
    float tLng = [self angleToRadians:toLoc.longitude];

    return atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
}  

Then just subtract your current compass heading to this heading (or vica-versa) and setRotation of your arrow/marker/view label or whatever.

Use this for angleToRadians:

-(float) angleToRadians:(float) a {
    return ((a/180)*M_PI);
}
Brad
So, if the width of the iphone in landscape mode is 460 pixels, the YscreenCenter value would be 230. And the width value is the distance from the screen to where? I can't understand what represents this value
alberto garcia
I understand you now, but my problem is that I don't have the width value(the distance to the screen center). I only have the position of the place relative to me (in degrees), and the other thing that I have is the compass heading (in degrees). So when the compass heading is equal to the position of the place the label is shown at the center of the screen. At this moment, if I turn the iphone to the right I need that the label turn to the left pointing to the place and vice versa. Sorry again, I've spent last week with this problem and I can't resolve it, so thank you very much for your help.
alberto garcia
1. Just use a constant as the "width". Pick "100" - It's just for display purposes.2. Do you mean you just need to "setRotation" on the Label?
Brad
right, I only need rotate the label in the direction of a point, just like an arrow in a compass, when you turn the compass the arrow continuos leading to the north, so my label has to rotate when I turn the iphone but it must indicate where is a particular place. In the ecuation that you wrote I also need the angle of the place where I want put the label.
alberto garcia
I understand. Revising "answer" to better reflect.
Brad
alberto garcia
Using the compass will tell you which was is north - but if you don't know where you *are* - how would you know which direction your destination is? You *need* the GPS to tell you where you *are* - and the compass to tell you which way you are *facing*. I saw the video - this type of application is called and *Augmented Reality* application.
Brad
because I have previously stored the degrees of the places in reference to a static (known)point where I am in the app.So in this case I only need the compass values. I'll try show you: I'm inside of a building, in a known position by the app and I'm pointing to the west. If I turn to the left, the compass will indicate that I'm looking to the south,east and so on...and my app knows that there is a label pointing the compass value=0(north), now I want the label appears in the left side of the screen when I turn to this value and disappear to the right side if I continuos turning. Thanks again
alberto garcia
I mean, I always use the app in the same place of the building, I won't move. I only rotate and I want the app shows me what things are around me. But I can't find the equation that translates the 360 degrees of the compass into the screen pixels like you did with the coordinates longitude and latitude.
alberto garcia
Okay - then you are only concerned with the "X" coordinate - as there is no "y" coordinate in dealing only with the compass. The iPhone camera seems to have a focal length which gives it about 70 degrees of view - so - knowing the difference in angle between where you are pointing and where you want to go - apply the formula: (angle*(320/70))+160. Angles from -35 degrees to 35 degrees, will yield an answer from 0 to 320. So if you were aimed directly where you wanted to go - the angle delta would be Zero degrees - and this formula would yield an answer of 160 - or the center of the screen.
Brad
Perfect!this is just what I need. I only have to calibrate more or less the degrees to the landscape mode but this is exactly what I was looking for. Thanks for your help Brad, you resolved me the problem.
alberto garcia
In theory - just change the "320" in the formula above to "480" to compensate for landscape - but I can't 100% positively say that this will be 100% aligned with the camera.
Brad
Ok, I'll try adjust it, maybe the focal length will be bigger too.
alberto garcia