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

时间:2009-10-28 05:42:05

标签: 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]);
        }
    }

问题场景。

位置管理员开始更新位置

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

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

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

2 个答案:

答案 0 :(得分:6)

CLLocationManaged具有委托方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

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

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

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

因此,您可以在委托方法中提出请求,以确保仅在更新当前位置后调用。

答案 1 :(得分:2)

我建议的一件事是在线程中执行你的urlRequest,而不是暗示Sixten Otto。

我会像viewDidLoadviewDidAppear一样设置locationManager。然后有两种方法我会称之为urlrequest:

  1. 请求在didUpdateToLocation委托中执行,然后您就会知道您的urlrequest只会在找到位置后执行。这是Morion提出的建议。

  2. 另一种方法是在didUpdateToLocation委托中有一个通知,这样当找到位置时,它会通知一个函数执行urlRequest.Look up Tutorial来设置通知,它是对很多程序都很有帮助,也很有用。

  3. 另外,正如我之前提到的,你应该在一个帖子中执行urlrequest。原因是,您的请求可能被锁定,您的用户无法控制退出请求。为此,请设置NSOperation并将其放在NSOperationQueue

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

    了解更多线程信息,请查看此Tutorial