2009-10-28 43 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