简体   繁体   中英

iOS didUpdateLocations stop

I want to track user movement every second and show it on Google Map. For this, I am using the below code. But this code fires multiple times every second.

First, I want to stop the multiple invocation of the method every second. Then when I pause in a certain location for 5 minutes, it should track the same latitude and longitude and insert in into my location array - I need that, when a user does not walk for couple of minutes (the same latitude and longitude does not enter in to my location array currently).

CODE:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //NSLog(@"didUpdateLocations");


    for (CLLocation *newLocation in locations) {
    //NSLog(@"in foor loop");
    NSDate *eventDate = newLocation.timestamp;

    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20) {
        // update distance
        if (self.locations.count > 0) {
            self.distance += [newLocation distanceFromLocation:self.locations.lastObject];

            CLLocationCoordinate2D coords[2];
            coords[0] = ((CLLocation *)self.locations.lastObject).coordinate;
            coords[1] = newLocation.coordinate;
            MKCoordinateRegion region =
            MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 500, 500);

           // [self.delegate updateRegion:region withOverLayCoordinates:coords];
        }

        [self.locations addObject:newLocation];
       // NSLog(@"self.locations:: %@",self.locations);
     }
  }
}
[locationManager stopUpdatingLocation];

使用此语法停止位置管理器更新。

The problem is, that your implementation is based on a timed geo position gathering, but that's not what Apple wants you to do. Here is why:

Imagine you have three apps running in the background collecting your location and acting upon it - you're walking in a city, having foursquare, step tracker and google maps navigation on. Suppose that all of them would request your position every second. That would drain the battery in matter of minutes. Rather the approach is, that all of the apps are subscribed to receive updates, when the location has changed, to use it. That's why you're receiving so many updates - because it's rapidly changing during the movement.

The first one is easy to fix - just store time in a local variable and ignore all other updates in that given interval. The second one is quite tricky. I know it's not exactly great news, but you'll have to find a way to combine post-processing and Apple's approach - eg duplicate the same position for as many times as there are seconds between now and the last time you received an update. Because there is currently no way to ask - "hey send me an update every second".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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