views:

519

answers:

2

I want to get my longitude and latitude on iphone in objective C. can any one guide me how to get these coordinates programmatically

A: 

Use the CLLocationManager, set an object as the delegate and start getting updates.

self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
self.locationManager.delegate = self; 
[self.locationManager startUpdatingLocation];

Then implement the delegate:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation { 
  NSLog([NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude]); 
  NSLog([NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude]); 
}
FigBug
A: 
#import <Foundation/Foundation.h>

@class CLLocationManager;

@interface CLLocationController : NSObject {
    CLLocationManager *locationManager; 
}

@property (nonatomic, retain) CLLocationManager *locationManager; 

@end

When i write above code is shows me following errors

/Hab/Folder/Classes/CLLocationController.m:10:30: error: CLLocationManager.h: No such file or directory /Hab/Folder/Classes/CLLocationController.m:21: warning: receiver 'CLLocationManager' is a forward class and corresponding @interface may not exist /Hab/Folder/Classes/CLLocationController.m:22: error: accessing unknown 'delegate' component of a property

You need to add the Core Location framework and then #import <CoreLocation/CoreLocation.h>
FigBug