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

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

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.

GPS and draw MapRout in iphone

I have a latitude and longitude that is fixed, so I used that and got the point in mapview with pin. Now I want the current location and draw route, so I think using gps I get the latitude and longitude. Using this location(latitude & longitude) I have to draw a route with my fixed value (latitude & longitude). How is it possible..? Is this is the right way to do it? ?How to draw the route with two point..?
I have used the delegate method but it doesn't call,
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingHeading];
NSLog(#"latitude=%f",newLocation.coordinate.latitude);
NSLog(#"longitude=%f",newLocation.coordinate.longitude);
}
I think the best way is using web service from google and draw the points as poly lines to the mapview. I am using http://maps.google.com/maps?f=d&hl=en&saddr=%lf,%lf&daddr=%lf,%lf&ie=UTF8&0&om=0&output=kml web service and draw the routes over mapView as poly lines.You will find an example here to draw polyline over some coordinates.

Why I am getting the altitude as 0

Why I am getting the altitude as 0 while using CoreLocation framework?
Altitude comes from GPS. You need to be outdoors to get altitude.
Take your device outdoor and see if you are getting altitude.
try this
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
altitudeLabel.text = [NSString stringWithFormat: #"%.2f m", newLocation.altitude];
}
Assuming you are testing your application on your device, the altitude will always be 0 when the update comes from Wi-Fi. Try disabling Wi-Fi to force an update from 3G. Note that it may take a while until you get an update because 3G doesn't return a position as fast as Wi-Fi.

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.