سؤال

Hi I am implementing Location services in my app. First I have to know my Coordinates to get the distance between some places that I have in a list and the device. Then if I go into a place I can make a check in, so, I need to get coordinates again, and the problem is here. Second time I try to get coordinates, the method -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations is not called.. and I can not get new Coordinates.

My manager is located in a NSObject sublcass with this code:

(id)init {
    if ( self = [super init] ) {
        if ([CLLocationManager locationServicesEnabled])
        {
            locationManager = [[CLLocationManager alloc] init];
            locationManager.delegate = self;
             [locationManager startUpdatingLocation];
        }
    }
    return self;
}

-(void) checkLongLatitudeAgain {
    [locationManager startUpdatingLocation];
}

#pragma mark Delegates de CLLocationManager
//
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
     NSLog(@"LON%f",  manager.location.coordinate.longitude);
    NSLog(@"LAT:%f", manager.location.coordinate.latitude);
    NSTimeInterval howRecentNewLocation = [newLocationeventDate timeIntervalSinceNow];

    if (manager.location.horizontalAccuracy <= 100.0 && howRecentNewLocation < -0.0 && howRecentNewLocation > -20.0){
        //Usar coordenada
        [self.delegate getLocationForCheckIn:manager.location];
        [self stopUpdatingLocation:@"Fins"];
    }
}
// ---------------------------------------------------------------------------------------------------------------------
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //
    if ([error code] != kCLErrorLocationUnknown) {
        [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")];
    }
    //
}
// ---------------------------------------------------------------------------------------------------------------------
- (void)stopUpdatingLocation:(NSString *)state {
    //Detenemos la lectura del GPS
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    NSLog(@"Stop gps");
    //
}

I call the class when the list of places is open, and also when inside a place the user press checkIn button. Both times I do it with this code:

WPLocationManager *location = [[WPLocationManager alloc]init];
[location checkLongLatitudeAgain];
هل كانت مفيدة؟

المحلول

You are creating a new manager every time:

WPLocationManager *location = [[WPLocationManager alloc]init];
[location checkLongLatitudeAgain];

That new manager is not assigned to any delegate.

You need to use the previous manager you have created and assigned to your delegate, something like:

[locationManager checkLongLatitudeAgain];

نصائح أخرى

You can check the documentation at http://developer.apple.com - https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

In particular you can check the Starting the Standard Location Service and Starting the Significant-Change Location Service sections. You have to use the startMonitoringSignificantLocationChanges or startUpdatingLocation method of CLLocationManager, cache your location somewhere and update it only when a new location is received, otherwise like it is stated in the documentation: "If a location update has already been delivered, you can also get the most recent location data directly from the CLLocationManager object without waiting for a new event to be delivered".

i dont know why you are initiating your location manager again again, also even if you some how manage to solve current problem but it's not proper way of dealing with location manage based applications.I had been in trouble previously when i was working on location based app. the best approach for location based app is singleton. apple forum discussion

you can find this and this very helpful.

just an advice, :) Thanks.

In iOS8 for me I had to call [locationManager stopUpdatingLocation]; before calling [locationManager startUpdatingLocation] to start getting updates second time and it works for me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top