简体   繁体   English

仅在第二次调用startUpdatingLocation之后获取当前位置

[英]Getting the current location only after the second time startUpdatingLocation is called

{
...
locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

        NSLog(@"myLocation1: %@",[locations lastObject]);
        myLocation = [locations lastObject];
        NSLog(@"myLocation2: %@",[locations lastObject]);
        [manager stopUpdatingLocation];

        [self doSomethingWithLocation];    
}

Currently I'm in the location 40.000,40.000. 目前我在位置40.000,40.000。 I'm closing my app and change location to 10.000,10.000 我正在关闭我的应用程序并将位置更改为10.000,10.000

When entering the app again and running [locationManager startUpdatingLocation]; 再次进入应用程序并运行[locationManager startUpdatingLocation]; my log will show: 我的日志将显示:

myLocation1: <+40.00000000,+40.00000000>
myLocation2: <+40.00000000,+40.00000000>

If I'll trigger [locationManager startUpdatingLocation]; 如果我触发[locationManager startUpdatingLocation]; again my log will show: 我的日志再次显示:

myLocation1: <+10.00000000,+10.00000000>
myLocation2: <+10.00000000,+10.00000000>

How can I call didUpdateLocations once and still get the current location? 我怎样才能一次调用didUpdateLocations并仍然获得当前位置? Should I use another delegate? 我应该使用其他代表吗?

I guess I could place stopUpdatingLocation inside doSomethingWithLocation and run doSomethingWithLocation after some sort of delay in order for the right location to be updated but I'm sure that's not the way it's meant to be. 我想我可以将stopUpdatingLocation放在doSomethingWithLocation并在经过某种延迟后运行doSomethingWithLocation以便更新正确的位置,但是我敢肯定这不是它的本意。

Thanks 谢谢

Leave the location manager running for a while (eg 30 seconds), setting a timer to tell it to stop. 让位置管理器运行一段时间(例如30秒),并设置计时器以使其停止。 The location manager updates are like pancakes, the first one you get isn't always the best. 位置管理器更新就像煎饼一样,您获得的第一个不一定总是最好的。

The first update you are seeing is likely a "stale" location, which was determined many minutes ago when location services were last powered up. 您看到的第一个更新可能是“陈旧”的位置,该位置是在几分钟前上次启动位置服务时确定的。 Or it may be a very inaccurate location determined using cell-tower positioning, for example. 或者,例如,它可能是使用蜂窝塔定位确定的非常不准确的位置。 If you just need to get the device's current location, using Core Location directly requires a good deal of code because you must handle these cases. 如果仅需要获取设备的当前位置,则直接使用Core Location需要大量代码,因为您必须处理这些情况。 (The CLLocationManager API appears to be built for apps that need continuous location updates, like turn-by-turn GPS navigation apps.) (CLLocationManager API似乎是为需要连续位置更新的应用程序而构建的,例如转弯GPS导航应用程序。)

Instead of using CLLocationManager directly, I suggest you take a look at using an open source component such as INTULocationManager which will handle all of this work for you and make it trivially simple to request one or more discrete requests for the device's current location. 我建议您不要使用直接使用CLLocationManager的方式,而是使用像INTULocationManager这样开放源代码组件 ,它将为您处理所有这些工作,并轻松地为设备的当前位置请求一个或多个离散请求。

In this case you should check timestamp of location. 在这种情况下,您应该检查位置的时间戳。 User does not move on such distances so quickly. 用户不会以如此快的距离移动。

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

        CLLocation *location = [locations lastObject];
        if(fabs([location.timestamp timeIntervalSinceNow]) < 5)//seconds
        {
            myLocation = location;
            manager.delegate = nil;
            [manager stopUpdatingLocation];
            [self doSomethingWithLocation];
        }
}

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

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