didUpdateToLocation doesn't gets called immediately

青春壹個敷衍的年華 提交于 2019-12-08 07:26:43

问题


I am using

startMonitoringSignificantLocationChanges

for getting the (lat,lon) values.My problem is

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

method doesn't gets called immediately after [locationMangerObject startMonitoringSignificantLocationChanges]; is encountered.Inside didUpdateToLocation method only i maintain two global double variables for holding latitude and longitude which will be set from the coordinates obtained from didUpdateToLocation method.These values are passed to the webservice,since didUpdateToLocation is called only a certain delay,between which the parameters for (lat,lon) takes zero as their value and passed to the service which results in an unexpected response.

How to make

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

method to get called immediately after `[locationMangerObject startMonitoringSignificantLocationChanges]; is encountered.

Please anybody suggest me a solution for resolving this issue.

Thanks in advance.


回答1:


Maybe you should consider using startUpdatingLocation method instead of startMonitoringSignificantLocationChanges. From iOS docs:

startUpdatingLocation

This method returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateToLocation:fromLocation: method.




回答2:


It can take several seconds to several minutes to get a good location depending on whether you have cell or wifi connection, and whether you have good signals from the GPS satellites. However, you should get a cached (possibly outdated) location right away.

Did you set the delegate for your locationMangerObject?

Did your function return to the runloop after calling the startMonitoringSignificantLocationChanges?




回答3:


///Hmmmmm

            ...CLLocationManager does n't call delegate method properly 
            ...after lot of R&D I'm  using a watchdog method it calls " -(void)locationadd " function every 10seconds and i'm using that location for drawing the path.

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



 - (void)viewDidLoad
     {

   [NSTimer scheduledTimerWithTimeInterval:10
                                         target:self
                                           selector:@selector(locationadd)
                                           userInfo:nil
                                           repeats:YES];

        }


        -(void)locationadd   
        {

         if ([locationManager location]) {



        //i'm saving this location

    CLLocation *locat=locationManager.location;

     }

        }
        //


来源:https://stackoverflow.com/questions/7156920/didupdatetolocation-doesnt-gets-called-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!