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 :)
Related
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.
I am writing an app, that uses GPS. I can get successfully the latitude, longitude and other properties, but altitude seems to always return "0.00" i have the following code to test it in the most simplest way and still get 0.00. Code below:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Stop updating location if renewed in less than 60 seconds
if ([self timeBetweenLocationandNow] < 60)
{
[locationManager stopUpdatingLocation];
NSLog(#"GPS Stopped");
}
NSLog(#"Altitude:%.2f m",newLocation.altitude);
}
Any ideas on what could be wrong ? also on an init method i have the following:
- (id)init
{
self = [super init];
if (self != nil)
{
// Create location manager Object
locationManager = [[CLLocationManager alloc] init];
// Set the delegate to this object
[locationManager setDelegate:self];
// Set distance filter and accuracy
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
return self;
}
Will appreciate any insight. thank you
I'm no expert but this is how I understand it: The iPhone is quite different from other gps systems because of the multiple ways in which it goes about figuring out your location. The iPhone figures out the location in three ways: local wifi signals, cell towers, and/or gps chip. Using a gps satellite to find a position can be extremely slow and reception can be pretty much non-existent in populated cities. Therefore, iPhone generally looks to the first 2 options first.
Wifi positioning data comes from people driving around on the streets logging hotspots which obviously isn't going to give much of a hint of an altitude. I'm not sure if cell tower triangulation gives any altitude data but my guess is no. These two methods are extremely fast compared to gps but will yield an altitude of 0 as you have seen. If you are in a place where there is a decent gps satellite reception the iPhone may give you the altitude, but even then, it might not even find it till it has received the satellite signal a few times and called the location delegate multiple times, and even then altitude data for gps can be notoriously inaccurate.
Long story short, it's a bad idea to count on accurate altitude data from the iPhone.
i found the problem. As Deepmist suggested the delegate has to be called several times. I was stopping the GPS after the first fix. Which was giving me accurate data for coordinates but altitude remained # 0.00 until like 3 or so delegate calls. Now i get the altitude data, what is a good way to check for this ? so i can stop the location manager after i get the data ? i can check that altitude is a non-zero value, but was wondering if there are other properties that should be used instead to make sure that you have the highest accuracy possible making all of your location data valid (i.e non-zero on some properties). thank you
I'm trying to calculate the distance between two places user Core Location. I've found a few posts that state to use
-(CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Found some other test code in the thread below:
CLLocationDistance NaN
I'm not sure how to put it all together, to get the result I want ?
Anyone any thoughts ?
Regards,
Stephen
If you have a location named myLocation and want to find the distance from another location, say, restaurantLocation, then it seems you would do something like
CLLocationDistance distance = [myLocation distanceFromLocation:restaurantLocation]
This will give you the distance, in meters, between the two.
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.
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.