gpt4 book ai didi

iphone - 等待准确的 didUpdateToLocation

转载 作者:行者123 更新时间:2023-11-29 03:43:28 26 4
gpt4 key购买 nike

我正在尝试为我的用户找到准确的纬度/经度坐标。我引用了文档并找到了 LocateMe 示例项目。我正在尝试编辑代码,以便弹出警报,让用户知道位置信息是否准确。

我有下面的代码,但警报总是说用户的位置信息不准确(“位置错误”)。有谁知道我做错了什么?

我刚刚意识到 Apple 的示例代码包含以下行: [setupInfo setObject:[NSNumber numberWithDouble:kCLLocationAccuracyHundredMeters] forKey:kSetupInfoKeyAccuracy];

我将如何编辑下面的代码以等待kCLLocationAccuracyNearestTenMeters。如果可能的话,我宁愿不必引用另一个 View Controller ,而是能够将其直接包含在方法中。

任何帮助都会很棒。谢谢你!

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

//CAN I PUT THE DESIRED ACCURACY HERE?
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m

[locationMeasurements addObject:newLocation];

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;

if (newLocation.horizontalAccuracy < 0) return;

if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy >
newLocation.horizontalAccuracy) {

self.bestEffortAtLocation = newLocation;

if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Good"
message:@"OK" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,
nil];
[alert show];
return;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Bad"
message:@"OK" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,
nil];
[alert show];
return;
}
}
}

最佳答案

将其放入委托(delegate)回调中就太晚了,因为在使用默认的desiredAccuracy后回调已经返回结果。

您可以将设置放在 init 方法中,然后在位置管理器上调用 start。

// set this before starting the calculations.
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];

我发现的唯一问题是它在用户体验流程中的放置,因为它会弹出警报,如果从用户体验角度来看在错误的时间完成可能会导致用户禁用此功能。如果应用程序的基础是使用位置服务,那么这并不好。 :)

希望这对您有所帮助。

关于iphone - 等待准确的 didUpdateToLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com