gpt4 book ai didi

iOS Compass App : Location coordinates keeps changing. 如何稳定?

转载 作者:行者123 更新时间:2023-11-29 02:48:58 24 4
gpt4 key购买 nike

我刚刚完成了指南针应用程序,它将显示 2 个坐标之间的距离。这是工作代码:

....

// created a timer to call locationUpdate method : 5sec
[NSTimer scheduledTimerWithTimeInterval:5 target: self selector: @selector(locationUpdate) userInfo: nil repeats: YES];
....

-(void)locationUpdate {

[locationManager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
//To get the current location Latitude & Longitude.
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;

startPoint = [[CLLocation alloc] initWithLatitude: latitude longitude: longitude ]; //Current Latitude and Longitude
endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.

//To get the distance from the 2 coordinates in feet
CLLocationDistance distInMeter = [startPoint distanceFromLocation:endPoint];

//Lable veiw to update remaining distance.
if(distInMeter > 999.000f) {
self.labelLongLat.text = [NSString stringWithFormat: @"Remainig distance %.2f KM with Lat : %lf LAN %lf", distInMeter / 1000, latitude, longitude ];
}else
self.labelLongLat.text = [NSString stringWithFormat: @"Remainig distance %.2f M Lat : %lf LAN %lf" , distInMeter, latitude,longitude ];
}

我的问题是每 5 秒更新位置时,坐标变化很大。这将导致 Remaining distance 计算。这是高度不稳定的!我该如何解决这个问题?

提前致谢

最佳答案

您正在使用的委托(delegate)方法已被弃用。您应该使用 locationManager:didUpdateLocations:

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

CLLocation *currentLocation = [locations lastObject];

endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.

//To get the distance from the 2 coordinates in meters

CLLocationDistance distInMeter = [currentLocation distanceFromLocation:endPoint];

//Label view to update remaining distance.
if(distInMeter > 999 ) {
self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f Km with Lat : %lf LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
} else {
self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f m Lat : %lf LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
}
}

您的位置准确性可能会受到信号质量(高层建筑、室内位置等)的影响。您可以检查您所在位置的 horizo​​ntalAccuracy 属性,以了解该位置的准确度。如果准确度较低,那么您可以推迟更新标签。请注意,您可能永远无法获得准确的修复。

一种策略是等待精度 <20m 或 5 次更新后 -

@property (nonatomic) NSInteger locationUpdates;

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

self.locationUpdates++;

CLLocation *currentLocation = [locations lastObject];

if (currentLocation.horizontalAccuracy < 20.0 || self.locationUpdates>5) {

endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.

//To get the distance from the 2 coordinates in meters

CLLocationDistance distInMeter = [currentLocation distanceFromLocation:endPoint];

//Label view to update remaining distance.
if(distInMeter > 999 ) {
self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f Km with Lat : %lf LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
} else {
self.labelLongLat.text = [NSString stringWithFormat: @"Remaining distance %.2f m Lat : %lf LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
}
}
}

关于iOS Compass App : Location coordinates keeps changing. 如何稳定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24772767/

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