如何每 4-5 秒获取距用户位置的距离

发布于 2024-09-12 05:46:45 字数 253 浏览 2 评论 0原文

我不会将其用于行车路线或类似用途。我有一些注释,并且希望当用户位于其中之一附近时触发,以便我可以提醒用户。

似乎每次 startUpdatingLocation 调用只调用一次 didUpdateToLocation ?至少当我在该方法中进行 NSLog 时,我只在控制台中得到一行。不,我不会带着 iPhone 到处走。

那么:设置对 userLocation 的连续监控并获得“回调”(当 3-4 秒过去或当用户移动(例如 10 米)时)的正确方法是什么?

I'm not using this for driving directions or similar. I have a few annotations and want a trigger when user is in the vicinity of one of those, so I can alert the user.

It seems didUpdateToLocation is called only once per startUpdatingLocation call? At least when I NSLog within that method, I only get one line in console. No, I'm not walking around with the iphone.

So: What is the correct way to set up a continuous monitoring of userLocation and get a "callback" (either when 3-4 seconds have passed or when user has moved say, 10 meters)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-09-19 05:46:45

您可能知道,这是初始化和启动位置管理器的代码:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

并像这样实现didUpdateToLocation

- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation 
{
   // This will be called every time the device has any new location information. 
}

每次有更新时,系统都会调用didUpdateToLocation地点。如果系统没有检测到位置的变化,didUpdateToLocation将不会被调用。您唯一能做的就是像我在示例中所做的那样设置 distanceFilterdesiredAccuracy 以获得最高的准确度。

更新

使用 kCLLocationAccuracyBest 而不是 kCLLocationAccuracyNearestTenMeters 以获得更高的准确性。

As you probably know, this is the code to initialize and start the location manager:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

And implement didUpdateToLocation like this:

- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation 
{
   // This will be called every time the device has any new location information. 
}

The system will call didUpdateToLocation every time there is an update to the location. If the system does not detect a change in location didUpdateToLocation will not be called. The only thing you can do is to set the distanceFilter and desiredAccuracy like i did in the example to give you the highest accuracy.

Update

Use kCLLocationAccuracyBest instead of kCLLocationAccuracyNearestTenMeters for more accuracy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文