簡體   English   中英

CLLocationManager不調用其委托方法

[英]CLLocationManager not calling its delegate method

我正在開發一個應用程序,其中涉及在X分鍾內跟蹤新位置並更新服務器。 當時間差等於或大於X分鍾時,我將停止位置更新,然后發送到服務器,如果小於X分鍾,則開始更新位置。 但是,當didUpdateToLocation委托方法少於X分鍾時,不會被調用。

我在這里發布我的代碼:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
{
   if (theDiff > 10.0f || myDate == nil) 
        {
            [self stopUpdating];
         }
        else
        {
            [self.mLocationManager startUpdatingLocation];
        }

}

嘗試這個。,

//in your .h

CLLocationManager *locationManager;

@property (nonatomic,retain) CLLocationManager *locationManager;

// Then synthesize it in your .m

@synthesize locationManager;

//in your viewDidLoad

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];

只要您隨手機一起移動,就會調用該方法。 您也可以通過更改模擬器的位置來進行檢查。

希望對您有所幫助。,謝謝,祝您編程愉快。

嘗試在.m中添加此方法,或者如果您有其他自定義代碼將其發布

-(void)LocationWithTime
{
    [CLController sharedInstance].locationManager.desiredAccuracy = 100.0;
    [CLController sharedInstance].delegate = self;
    BOOL locationAccessAllowed = NO ;

    if( [CLLocationManager respondsToSelector:@selector(locationServicesEnabled)] )
    {
        locationAccessAllowed = [CLLocationManager locationServicesEnabled] ;
    }
    if(locationAccessAllowed == YES)
    {

        [[CLController sharedInstance].locationManager startUpdatingLocation];
    }

}

這是我使用的委托方法-

-(void)newLocationUpdate:(NSString *)latvalue longValue:(NSString*)longvalue Error:(int) errCode
{



    DebugLog(@"LatLong values %@ %@", [UserContext sharedInstance].strLat, [UserContext sharedInstance].strLong);

    self.strLALat = latvalue;
    self.strLALong = longvalue;

    [[CLController sharedInstance].locationManager stopUpdatingLocation];
    [CLController sharedInstance].delegate=nil ;
}

摘自Apple API文檔,

/ * * locationManager:didUpdateToLocation:fromLocation:*
*不推薦使用此方法。 如果*實現了locationManager:didUpdateLocations:,則不會調用此方法。 * /

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0);

請改用以下委托方法,

/ * * locationManager:didUpdateLocations:* location是按時間順序排列的CLLocation對象的數組。 * /

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);

設置委托后,此方法將起作用。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM