Verify that the CLLocationManager instance isUpdatingLocation

I need to know if the instance CLLocationManager(in this case called gps) updates the location by calling the appropriate method

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

In my case, the full method:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    address.text = NULL;
    [activityIndicator setHidden:NO];
    [activityIndicator startAnimating];
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];
            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];


            if (address.text != NULL)
            {
                gpsTimer = [NSTimer scheduledTimerWithTimeInterval:10
                                                            target:gps
                                                          selector:@selector(startUpdatingLocation)
                                                          userInfo:nil
                                                           repeats:NO];
                [address sizeToFit];
                [gps stopUpdatingLocation];
            }
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

How can i do this?

Thank you in advance:)

+4
source share
4 answers

subclass your CLLocationManager and add a BOOL that is set to yes when calling the delegate method ...

You can also simply tell your location manager to stay in the delegate method

+8
source

The following is a simple code example showing how to deal with this problem.

import Foundation
import CoreLocation

public class LocationManager : NSObject, CLLocationManagerDelegate{  

    var locationManager: CLLocationManager?
    public var isLocationUpdate: Bool = false


     override public init() {
        super.init()

        self.locationManager = CLLocationManager()
        self.locationManager!.delegate = self

        if #available(iOS 9.0, *) {            
            self.locationManager!.requestWhenInUseAuthorization()          
        }
        self.locationManager!.startUpdatingLocation()
        self.isLocationUpdate = false
    }


public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   self.isLocationUpdate = true
  print(locations)

}


public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

        //Retrieving the error code.
      if let code = (error as NSError).code{
        isLocationUpdate = false
      }
}
+2
source

, "" ? ".

, , .

, , , :

1- , , , requestlocationupdates .

2- , , , - , , -:

[self performSelector:@selector(fetchLocationTimeout) withObject:nil afterDelay:LOCATION_LISTEN_TIME];

:

(void)fetchLocationTimeout{
    if(!finished){
        [self stopUpdatingLocation];    
        [delegate locationFinished:bestEffortAtLocation];//see more on this below
    }
}

3- didupdate- "" , , " ", , :

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

    if(newLocation is the best i have){

        self.bestEffortAtLocation = newLocation;

    }
// test the measurement to see if it is more accurate than the previous measurement

    if (bestEffortAtLocation == nil || newLocation is the best i have) {
// store the location as the "best effort"
        if (bestEffortAtLocation is good enough and i want to use it) {        
            [self stopUpdatingLocation];            
        // we can also cancel our previous performSelector:withObject:afterDelay: - it no longer necessary
        SEL selector = NSSelectorFromString(@"fetchLocationTimeout");
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
            [delegate locationFinished:bestEffortAtLocation];
        }
    }
}

, , , didupdate, , :

(void)stopUpdatingLocation{
    finished = true;
    NSLog(@"stopUpdatingLocation! manager: %@", [locationManager description]);
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
}

: "", , , - , , , . , "locationFinished".

, , .

0

Apple iOS 6, , iOS, , :

iOS 2 - iOS 5:

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

iOS 6 :

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
0

All Articles