Getting Direction from Compass - iphone

How do i get the Direction from compass when i rotate my device?
i have use :
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
}
method but nothing is detected under heading tag?
i just want to get the direction (i.e) 90N , 180S etc..
need help
Thank you.

There is no direction property, just the heading from north. You have to calculate the direction by yourself.

Related

How to show a location with respect to my current location?

I have a lat and longitude of my friend's location also my current location.
I want to show the position of my friend with respect to my location, just like a compass - in an iPhone app.
I know the function didUpdateHeading -
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSLog(#"New magnetic heading: %f", newHeading.magneticHeading);
NSLog(#"New true heading: %f", newHeading.trueHeading);
}
I am not getting an idea about how to do this, Please help me.
If nobody else provides a better solution here is how to do it:
Your point is A, your friend point is B, and the common point is whatever 0,0 , a common reference : C
Use distanceFromLocation function.
With that point you will know the 3 triangle side.
Now go to wikipedia or back to 6th class school how to calculate the angles on triangle if you have the side values.
You need 1 of the angle.
I am lazy to to search and write a ready for copy-paste code, because I know you will not up-vote this either :)

How do I calculate my distance from where I am now to a specific point

On my app, I have pin pointed an area on interest. And also the GPS of my location, how would I now let the app calculate and show the user how far the distance away is for them?
My .h file is setup with a map view, a button for finding my location and set map options to choose between map/satellite and hybrid views.
In my .m file I have all the code setting the location and where I am, and also enabled tracking mode. Also setup the different map views and have set a pin point on my map and set the latitude and longitude, then enable Zoom & Scroll function. I have also set so that on the map, when you click on the pinpointed area it says the name and a small description of the place. So everything works fine at this point :D
I really need to find how to find the distance between my point and the pinpointed area.
*please note I am using MapKit!
here if you use bellow code in delegate method of CLLocationManager then its return the distance from your current point to destination point..
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *appleHQ = [[CLLocation alloc] initWithLatitude:37.322998 longitude:-122.032182];////here put your destination point
NSLog(#"New Location:%#", newLocation);
CLLocationDistance distance = [newLocation distanceFromLocation:appleHQ];
NSLog(#"Distance to Apple HQ %4.0f km", distance);
[appleHQ release];
}
just try this code ...
hope,this help you.

Find if device is pointing in the right heading / direction?

I would like to find the distance to a location and which direction the device is heading in relation to that location. I've got the distance working but cant work out how to find the which way the location.
_theLocation = [[CLLocation alloc]initWithLatitude:-41.561004 longitude:173.849030];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationDistance meters = [newLocation distanceFromLocation:_theLocation] * 0.000621371192;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSString *str = [NSString stringWithFormat:#"Heading = %.f", newHeading.trueHeading];
}
Is it possible to find the heading to _theLocation from our location?
Can't you simply capture the device's location at two instances & figure out the direction wrt the location?
Not a complete solution, but assuming that the device only moves along the line joining the device & the location, you can use – distanceFromLocation:. If you capture the distance from the location at two instances & see if the latter value is less than the former, you can deduce that the device has moved closer to the location.
In fact, from your question, it appears that you want to determine whether the device has moved closer to the location or not (and not the exact direction, say N or NE). If that's the case, then this should work.
HTH,
Akshay

Implementing a horizontal compass on the iPhone - algorithm?

A horizontal compass looks something like this if you are facing due East (90 degrees).
85----90---95
If you were facing due 355 degrees northwest, it would look like this:
350----355---0
As you turn the compass, the number should cycle from 0 -> 360 -> 0
So, my question is, how would you implement a view like this on the iPhone? I had a couple of ideas:
Make one long image with all numbers
and tick marks, and shift it
left/right when the compass heading
changes
Create pieces of the view as tiles
and append them when the compass
heading changes.
Create a line of tick marks that
shifts with the compass heading, and
just write numbers on it as needed.
How would you attack this problem? Im mainly looking for algorithmic advice, but if you ave code or pseudo-code to demonstrate, that would be helpful too.
Option one is the easiest. Keep in mind that you can composite part of an image to deal with the wrap-around.
Here is a complete solution of implementing horizontal compass in iPhone 4
if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable])
{
m_locationManager=[[CLLocationManager alloc]init];
m_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
m_locationManager.headingOrientation=CLDeviceOrientationPortrait;;
m_locationManager.delegate=self;
[m_locationManager startUpdatingHeading];
}
pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
CLLocationDirection direction= newHeading.magneticHeading;
[m_readingsLabel setText:[NSString stringWithFormat:#"Degrees:%f",direction]];
float radians=direction*M_PI/180;
m_compassImageView.transform=CGAffineTransformMakeRotation(radians);
}
You can build the modulo 10 of the heading and switch for 10, 5 and all others.
You just need to implement location manager functionality to find the exact direction in which your iPhone is pointing.
So, You can check this :
CLLocationManager *locationManager;
[locationManager startUpdatingHeading];
And whenever you call startUpdatingHeading() it will call
"- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading" method which you need to override from Location manager.
And that's it you can find the direction in which iPhone 3GS is pointing by newHeading.magneticHeading or newHeading.trueHeading.
You will surprise that you can get exact the same direction what magnetic compass gives you.

CLLocationManager ensure best accuracy for iphone

Does anyone have any suggestions on how to obtain the best horizontal accuracy on the location manager? I have set the desired accuracy to NearestTenMeters, but given that the accuracy can always change depending on coverage area, how can I write some code in the locationManager to stop updating only after I get the best horizontal accuracy?
If you want it to be to a certain accuracy, set the delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
to have
if(newLocation.horizontalAccuracy <= (some number) )
[locationManager stopUpdatingLocation];
something like that.
Typically the longer the hardware is on and the more locations it gets the more accurate it gets. You can try turning on the manager early to let it get accurate and then grab the location. It also helps when the target is moving, the chip is able to get more accurate readings when gps is moving. Also the location of the device makes a diffrence, you will get much better reads if you are outside on a field than if you are in a bunker, so sometimes the read you get is the best thats available. Your code can wait till it gets no more updates from location manager for some time, then its probably at its best accuracy.