3

I have one trouble in my app i want to know the direction to the Latitude and Longtitude from the current Lat & Long

-(void)showDirection
{
 CGFloat latitude = Lat;
 if (latitude < 0) {
    latitude = -latitude;
    strDirection = @"S";
 } else {
    strDirection = @"N";
 }
 // Longitude
 CGFloat longitude = Long;
 if (longitude < 0) {
    longitude = -longitude;
    strDirection = @"W";
 } else {
    strDirection = @"E";
 }
 NSLog(@"direc %@",strDirection);
}
ziesemer
  • 27,712
  • 8
  • 86
  • 94
iBhavik
  • 663
  • 11
  • 28
  • HOpe this link can solve your problem properly, http://stackoverflow.com/questions/412256/can-the-iphone-determine-if-youre-facing-north-south-east-or-west – Ankit Gupta Apr 20 '12 at 11:58
  • Possible duplicate of [How to get directions on the iPhone or iPad](http://stackoverflow.com/questions/2630789/how-to-get-directions-on-the-iphone-or-ipad) – Suhaib Oct 27 '16 at 05:22

2 Answers2

8

try this code below:

first add framework in your project CoreLocation.Framework after adding initialize CLLocationManager object and import CLLocationManager header file.

Yourviewcontroller.h

#import <CoreLocation/CoreLocation.h>
@interface Yourviewcontroller :UIViewController <CLLocationManagerDelegate>
{
      CLLocationManager *locationManager;
} 

Yourviewcontroller.m

locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
//Start the compass updates.
[locationManager startUpdatingHeading];

add function for get current direction

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    float mHeading = newHeading.magneticHeading;
    if ((mHeading >= 339) || (mHeading <= 22)) {
        //[direction setText:@"N"]; 

    }else if ((mHeading > 23) && (mHeading <= 68)) {
        //[direction setText:@"NE"];    

    }else if ((mHeading > 69) && (mHeading <= 113)) {
        //[direction setText:@"E"]; 

    }else if ((mHeading > 114) && (mHeading <= 158)) {
        //[direction setText:@"SE"];

    }else if ((mHeading > 159) && (mHeading <= 203)) {
        //[direction setText:@"S"]; 

    }else if ((mHeading > 204) && (mHeading <= 248)) {
        //[direction setText:@"SW"];    

    }else if ((mHeading > 249) && (mHeading <= 293)) {
       // [direction setText:@"W"];

    }else if ((mHeading > 294) && (mHeading <= 338)) {
       // [direction setText:@"NW"];

     }
}

Note :compass is work only real iphone device,not work in iphone simulator..!

efdalustaoglu
  • 168
  • 11
Dinesh
  • 6,500
  • 10
  • 42
  • 77
-4

You could try LocateMe. It will give you the long and lat of the user's current position.
Or your could try looking at this tutorial

Hope this helps!

Zack
  • 5,108
  • 4
  • 27
  • 39