简体   繁体   English

CLLocationManager不调用其委托方法

[英]CLLocationManager not calling its delegate method

I am developing an app which involves tracking the new location and updates to the server in X mins. 我正在开发一个应用程序,其中涉及在X分钟内跟踪新位置并更新服务器。 I am stopping the location updating when the time diff is equal to or greater than X mins, then sending to the server and then start updating the location if it is less than X mins. 当时间差等于或大于X分钟时,我将停止位置更新,然后发送到服务器,如果小于X分钟,则开始更新位置。 But the didUpdateToLocation delegate methos is not called when it is less than X mins. 但是,当didUpdateToLocation委托方法少于X分钟时,不会被调用。

I am posting my code here: 我在这里发布我的代码:

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

}

try this., 尝试这个。,

//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];

This will call the method whwnever you move with your phone. 只要您随手机一起移动,就会调用该方法。 You can check this by changing simulator locatin also. 您也可以通过更改模拟器的位置来进行检查。

Hope this helps., Thanks, happy coding. 希望对您有所帮助。,谢谢,祝您编程愉快。

try this in .m add this method , or if you have any other custom code post it 尝试在.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];
    }

}

this is the delegate method i have used - 这是我使用的委托方法-

-(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 ;
}

Excerpt from Apple API Documentation, 摘自Apple API文档,

/* * locationManager:didUpdateToLocation:fromLocation: * / * * locationManager:didUpdateToLocation:fromLocation:*
* This method is deprecated. *不推荐使用此方法。 If locationManager:didUpdateLocations: is * implemented, this method will not be called. 如果*实现了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);

Use the below delegate method instead, 请改用以下委托方法,

/* * locationManager:didUpdateLocations: * locations is an array of CLLocation objects in chronological order. / * * locationManager:didUpdateLocations:* location是按时间顺序排列的CLLocation对象的数组。 */ * /

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

This will work once you set the delegate. 设置委托后,此方法将起作用。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM