How to wait for the location manager for the current location?

I am developing an iPhone application in which I want to show the nearest restaurants based on the current location For this In applicationDidFinishLaunching I do this:

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

      

Problematic scenario.

the location manager will start updating the location

The webservice starts before that, so I can't get the location values.

I put the web service call in a delegate method, then the application is started before the web service is done. in the delegate method, I set the latitude and longitude on the appropriate lines.

The problem is how to ensure that the service doesn't get called until the location manager updates that location, then passes the location to the web service, and then calls that web service.

+2


source to share


2 answers


CLLocationManaged has a delegate method.

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

      

it will be called when CLLocationManager receives some coordinates. Or



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

      

it will be called when locationManager cannot get any coordinates. Or when the user is unable to find the current location. You should also call stopUpdatingLocation if you don't want more accurate results.

This way you can make your request inside the delegate method sure to be called only after updating the current location.

+6


source


One thing I would recommend is to make your urlRequest on a stream instead, as Sixten Otto hinted at.

I would set locationManager to viewDidLoad

or whatever viewDidAppear

you like. Then there are two paths that I would call urlrequest:

  • Ask to execute the request in the didUpdateToLocation delegate, then you will know that your urlrequest will only be executed after the location is found. This is the offer that Morion made.

  • Another way is to notify in didUpdateToLocation deal so that when location is found it will notify function to execute urlRequest. Take a look at this Tutorial for setting notifications, it is very useful and useful to know many programs.



Also, as I mentioned earlier, you have to urlrequest on the stream. The reason is that your request may be blocked and your user will not have any control to exit the request. To do this, install NSOperation and place it in NSOperationQueue

:

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

      

for more information on streams, have a look at this Tutorial

+2


source







All Articles