CoreLocation文档应该回答您的任何问题。但是要获取手机的当前位置:
// based on http://www.icodeblog.com/tag/corelocation/
@interface CFAAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
//Add a location manager property to this app delegate
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation CFAAppDelegate
@synthesize window = _window;
@synthesize locationManager=_locationManager;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
if(self.locationManager==nil){
_locationManager=[[CLLocationManager alloc] init];
//I'm using ARC with this project so no need to release
_locationManager.delegate=self;
_locationManager.purpose = @"We will try to tell you where you are if you get lost";
_locationManager.desiredAccuracy=kCLLocationAccuracyBest; // other options exist, let's assume this one
_locationManager.distanceFilter=500;
self.locationManager=_locationManager;
}
return YES;
}
- (void)awakeFromNib {
if([CLLocationManager locationServicesEnabled]){
[self.locationManager startUpdatingLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
//Location seems pretty accurate, let's use it!
NSLog(@"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
}
将其报告给数据存储是留给读者的练习。