【问题标题】:How to wait for location manager for the current location?如何等待当前位置的位置管理器?
【发布时间】:2009-10-28 05:42:04
【问题描述】:

我正在开发一个 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 服务之前不会调用该服务..

【问题讨论】:

  • 我能给出的最重要的建议是:不要像这样在你的应用委托中做所有这些,也不要试图强迫你的应用与网络同步交互。它可能是不那么复杂的代码,但它是一个更糟糕的用户体验。默认情况下,核心位置和 URL 加载系统都是异步的。拥抱它!
  • 我不明白你的想法。你能详细说明一下吗?

标签: iphone core-location


【解决方案1】:

CLLocationManaged 具有委托方法。

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

当 CLLocationManager 接收到一些坐标时会调用它。或者

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

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

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

【讨论】:

  • ok didUpdatetoLocation 准确地完成了这件事,但位置管理器需要更多时间,在此之前我的网络服务会被执行。我希望网络服务代码先等待位置管理器找到位置,然后执行该网络服务代码。
  • 如果您将在 didUpdateToLocation 方法中向 Web 服务发出请求,它将在您获得一些坐标后调用。或者在您开始更新位置的同一个metmod中向您的服务发出请求非常关键?
  • "如果您将在 didUpdateToLocation 方法中向 Web 服务发出请求,它将在您获得一些坐标后调用。"如您所说,请在代码中说明是否可行。
【解决方案2】:

我建议的一件事是在线程中执行您的 urlRequest,而不是按照 Sixten Otto 的提示。

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

  1. 如果在didUpdateToLocation 委托中执行请求,那么您将知道您的urlrequest 只会在找到位置后执行。这是莫里安的建议。

  2. 1234563并且很高兴知道很多程序。

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

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

更多线程信息,请查看Tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多