@property (nonatomic,strong) CLLocationManager *locManager;
这是我设置经理的地方:
-(void) viewDidAppear:(BOOL)animated
{
self.locManager = [[CLLocationManager alloc] init];
[_locManager setDelegate:self];
[_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[_locManager setPausesLocationUpdatesAutomatically:NO];
[_locManager setDistanceFilter:1.0];
[_locManager startUpdatingLocation];
}
这是我的委托方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
lonLabel.text = longt;
}
该方法在启动时被调用三次,然后再也不会被调用。 提前谢谢
答案 0 :(得分:1)
不推荐使用上述方法。 请使用此代表
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;
[locationManager stopUpdatingLocation];
locationManager = nil;
}
答案 1 :(得分:0)
将这段代码保存在viewDidLoad:
self.locManager = [[CLLocationManager alloc] init];
答案 2 :(得分:0)
使用此方法:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(@"updated coordinate are %@",location_updated);
}
此方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
...已在iOS 6.0中弃用