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

Iphone 如何等待当前位置的位置管理器?,iphone,core-location,Iphone,Core Location,我正在开发一个iPhone应用程序,我想在其中根据当前位置显示最近的餐馆 为此,在ApplicationIDFinishLaunching中,我将执行以下操作: self.locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locati

我正在开发一个iPhone应用程序,我想在其中根据当前位置显示最近的餐馆 为此,在ApplicationIDFinishLaunching中,我将执行以下操作:

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]);
        }
    }
问题场景

位置管理器将开始更新位置

webservice在此之前执行,因此我无法获取位置值

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


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

CLLocationManaged具有委托方法

当CLLocationManager接收到一些坐标时,将调用它。或

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


因此,您可以在委托方法中进行请求,以确保只有在更新当前位置后才会调用is。

我建议您在线程中执行urlRequest,而不是像提示的Sixten Otto那样

我会像您一样在
viewDidLoad
viewDidAppear
中设置locationManager。然后有两种方法可以调用urlrequest:

  • 如果在didUpdateToLocation委托中执行请求,那么您将知道只有在找到位置后才会执行urlrequest。这是莫里昂提出的建议

  • 另一种方法是在didUpdateToLocation委托中有一个通知,这样当找到该位置时,它会通知一个函数来执行urlRequest。查看此设置通知,了解许多程序非常有用

  • 另外,正如我前面提到的,您应该在线程中执行urlrequest。原因是,您的请求可能被锁定,并且您的用户无法控制退出请求。为此,请设置NSOperation并将其放入
    NSOperationQueue

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget: self 
                                                                                    selector: @selector(METHOD:) 
                                                                                      object: OBJECTorNIL;
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [queue addOperation:operation];
    

    有关更多线程信息,请查阅此

    ok didUpdatetoLocation,它可以准确地执行任务,但location manager需要更多的时间,在执行web服务之前。我希望web服务代码等待位置管理器首先找到位置,然后执行该web服务代码。如果您将在didUpdateToLocation方法中向web服务发出请求,则在获得一些坐标后将调用它。或者,在同一个metmod中向您的服务发出请求是非常关键的,您在那里开始更新位置?“如果您将在didUpdateToLocation方法中向web服务发出请求,则在获得一些坐标后将调用它。”如何按照你所说的去做,如果可能的话,请用代码告诉我。我能给出的最重要的建议是:不要在你的应用程序中这样做,不要试图强迫你的应用程序与网络同步交互。代码可能不那么复杂,但用户体验却差得多。默认情况下,核心位置和URL加载系统都是异步的。拥抱它!我不明白你的意思。你能详细说明一下吗。
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget: self 
                                                                                    selector: @selector(METHOD:) 
                                                                                      object: OBJECTorNIL;
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [queue addOperation:operation];