CLLocationManager重复几次

时间:2016-01-14 12:25:45

标签: ios cllocationmanager gmsmapview

有点问题。我的应用程序中有一个GMSMapView,它指向加载时的用户位置。当app加载时,我将locationManager设置为在找到用户位置后在日志中显示纬度。现在它显示纬度超过3次(我计划将此帖子发送到我的服务器)。当我启动我的应用程序时,我将拥有相当多的用户,并且每个用户额外的3个服务器请求对于服务器来说将是很多。我想知道在代码中我做错了什么或者有办法解决这个问题吗?将附上以下代码。

谢谢!

- (void)viewDidLoad
{

    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:(id)self];
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startMonitoringSignificantLocationChanges];
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [self.locationManager stopUpdatingLocation];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
                                                            longitude:newLocation.coordinate.longitude
                                                                 zoom:17.0];
    [self.mapView animateToCameraPosition:camera];
    latitude = newLocation.coordinate.latitude;
    longitude = newLocation.coordinate.longitude;
    NSLog(@"latitude = %g", latitude);


}

1 个答案:

答案 0 :(得分:0)

didUpdateToLocation在iOS6中被弃用了。使用替换的didUpdateLocations,然后选择数组的最后一个元素,例如:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    location = [locations lastObject];
    // ...
    latitude = location.coordinate.latitude;
    longitude = location.coordinate.longitude;