镔哥主要寫幾個步驟,希望能幫到大家:
前提導入:CoreLocation.farme
#import
代理
{
//實現獲取當前地理位置:第一步:
//這兩個變量,locationManaager用於獲取位置,checkinLocation用於保存獲取到的位置信息
NSString * currentLatitude;
NSString * currentLongitude;
CLLocationManager *locManager;
CLLocation *checkinLocation;
}
#pragma mark--CLLocationManagerDelegate
/*
第二步:
實現地理位置實現第一個代理方法
運行到這個方法的時候,locationManager已經得到了當前的位置,所以在這個方法中應該把獲取到的當前位置保存到變量checkinLocation中*/
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
checkinLocation = newLocation;
//do something else
}
/*
第三步:
在第二步實現的方法其實是被CLLocationManager中的方法startUpdatingLocation調用的,也就是說,當程序運行 locationManager.startUpdatingLocation的時候,第二步實現的方法就會被調用
*/
- (void) setupLocationManager {
locationManager = [[CLLocationManager alloc] init] ;
if ([CLLocationManager locationServicesEnabled]) {
NSLog( @"Starting CLLocationManager" );
locationManager.delegate = self;
locationManager.distanceFilter = 200;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
} else {
NSLog( @"Cannot Starting CLLocationManager" );
/*self.locationManager.delegate = self;
self.locationManager.distanceFilter = 200;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];*/
}
}