简体   繁体   English

iOS didUpdateLocations停止

[英]iOS didUpdateLocations stop

I want to track user movement every second and show it on Google Map. 我想每秒跟踪一次用户移动并将其显示在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). 然后,当我在某个位置暂停5分钟时,它应该跟踪相同的纬度和经度,然后插入到我的位置数组中-我需要这样做,当用户几分钟不走动时(相同的纬度和经度不会当前输入我的位置数组)。

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. 问题是,您的实现是基于定时地理位置收集的,但这不是Apple希望您执行的操作。 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. 想象一下,您在后台运行了三个应用程序来收集您的位置并对其进行操作-您正行走在城市中,具有foursquare,step tracker和google maps导航。 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. 我知道这并不是一个好消息,但是您必须找到一种将后处理与Apple的方法相结合的方法-例如,从现在到上次收到更新之间的时间间隔,重复相同的位置要进行多次。 Because there is currently no way to ask - "hey send me an update every second". 因为目前没有办法询问-“嘿,每秒发送一次更新给我”。

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

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