2009-10-28 47 views
2

我正在开发中,我想展示基于当前位置 对于在的applicationDidFinishLaunching我做这个最近的餐馆的iPhone应用程序:如何等待当前位置的位置管理器?

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服务..

+2

最重要的建议,我可以给:不要做这一切在你的应用程序代理像这样,不要试图强迫你的应用程序与网络同步交互。这可能不太复杂的代码,但它是一个更糟糕的用户体验。核心位置和URL加载系统在默认情况下都是异步的。拥抱那个! – 2009-10-28 06:19:47

+0

我不明白你的想法。你能否详细说明一下。 – harshalb 2009-10-28 06:38:55

回答

6

CLLocationManaged有委托方法。

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

它会被称为当CLLocationManager会收到一些坐标。或

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

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

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

+0

ok didUpdatetoLocation准确地完成了事情,但位置管理器需要更多时间,并且在此之前,我的Web服务得到执行。我希望Web服务代码等待位置管理器先找到位置,然后执行该Web服务代码。 – harshalb 2009-10-28 05:57:09

+0

如果您将在didUpdateToLocation方法中向Web服务发出请求,则会在您有一些坐标后调用它。或者,在开始更新位置的同一个metmod中向您的服务提出请求非常关键? – Morion 2009-10-28 06:17:20

+0

“如果您将在didUpdateToLocation方法中向Web服务发出请求,则会在您有一些坐标后调用它。” 如你所说,请按照代码告诉我们是否有可能。 – harshalb 2009-10-28 06:34:35

2

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

我会像设置viewDidLoadviewDidAppear那样设置locationManager。那么有两种方法,我会打电话的URLRequest:

  1. 有请求didUpdateToLocation委托执行,那么你就会知道该位置被发现后您的URLRequest将只执行。这是Morion提出的建议。

  2. 另一种方法是,这样当发现位置有在didUpdateToLocation委托的通知,它会通知执行urlRequest.Look了这个Tutorial用于设置通知的功能,这是非常有益的和好知道很多节目。

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

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

更多的线程信息,查找此Tutorial

+0

谢谢你的回答。让我检查链接。 – harshalb 2009-11-25 14:57:14