如何等待当前位置的位置管理器?

har*_*alb 2 iphone core-location

我正在开发一个iPhone应用程序,其中我想根据当前位置显示最近的餐馆为此在applicationDidFinishLaunching我这样做:

self.locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];



    NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.0.150/server1/Service.asmx/nearest?lat1=23.013163&lon1=72.559068"];
    NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:url];
    [request2 setHTTPMethod:@"GET"]; 
    [request2 setTimeoutInterval:10];
    NSURLResponse *response=nil;
    NSError *err=nil;
    NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
    if(data1 == nil)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    else
    {
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data1];

        //Initialize the delegate.
        XMLParser *parser = [[XMLParser alloc] initXMLParser];

        //Set delegate
        [xmlParser setDelegate:parser];

        //Start parsing the XML file.
        @try {
            BOOL success = [xmlParser parse];
            if(success)
                NSLog(@"No Errors");
            else
                NSLog(@"Error Error Error!!!");
        }
        @catch (NSException * e) {
            NSLog(@"Exception in parsing %@  %@",[e name], [e reason]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题场景.

位置管理员开始更新位置

web服务在此之前执行,因此我无法获取位置值.

我正在将委托方法中的Web服务调用放入,然后在Web服务执行之前启动应用程序.在委托方法中,我在适当的字符串中设置纬度和经度.

问题是如何确保在位置管理器更新该位置然后将该位置传递给Web服务然后调用该Web服务之前不会调用该服务.

Mor*_*ion 6

CLLocationManaged具有委托方法.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
Run Code Online (Sandbox Code Playgroud)

当CLLocationManager将接收一些坐标时将调用它.要么

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

当locationManager无法接收任何坐标时将调用它.或者当用户不允许查找当前位置时.如果您不想收到更准确的结果,也应该调用stopUpdatingLocation.

所以你可以在委托方法中提出你的请求,以确保只有在更新当前位置后才会调用它.