gpt4 book ai didi

ios - UIlabel不更新用户当前速度

转载 作者:行者123 更新时间:2023-11-29 03:41:56 24 4
gpt4 key购买 nike

这个问题已经困扰了几天了,但似乎无法找到解决方案。这可能是一些非常基本的东西,但仍然无法提出解决方案。所以我试图让我的 uilabel 更新汽车或自行车上的用户速度。但是当 UIlabel 更新时,它不会更新正确的值。任何帮助将不胜感激。这是我的 .h 文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate, NSObject>
{
AppDelegate *appDelegate;
IBOutlet MKMapView *userMap;
CLLocationManager *locationManager;

}
@property (strong, nonatomic) IBOutlet UILabel *speedView;
@property(nonatomic) int speedCount;
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic, strong) WEPopoverController *popoverController;

+ (NSString *) speedToMPH: (float) value;


- (IBAction)btnMenuTapped:(id)sender;

@end

和我的 .h 文件

@implementation MainViewController
@synthesize speedCount;
@synthesize speedView;
@synthesize popoverController;
@synthesize locationManager;


- (void)locationError:(NSError *)error {
speedView.text = [error description];
}



-(void)viewWillAppear:(BOOL)animated
{
[userMap setRegion:MKCoordinateRegionMakeWithDistance(userMap.userLocation.coordinate, 5, 5) animated:YES];
}

- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [[UIApplication sharedApplication] delegate];
locationManager =[[CLLocationManager alloc] init];
// Do any additional setup after loading the view, typically from a nib.
[userMap setCenterCoordinate: userMap.userLocation.coordinate animated: YES];
[self performSelector:@selector(checkForLogin) withObject:nil afterDelay:1];

[self startLocationServices];
// create LM
self.locationManager = [CLLocationManager new];

// set its delegate
[self.locationManager setDelegate:self];

// set configuration
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];

// start location services
[self.locationManager startUpdatingLocation];
// create an oldLocation variable if one doesn't exist
}


- (void)startLocationServices
{
// create the Location Manager
if (self.locationManager == nil) {
self.locationManager = [CLLocationManager new];
}

// stop services
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
self.speedView.text = @"Location Services stopped.";
}
// locationManager didUpdateToLocation FromLocation
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
self.speedView.text = [NSString stringWithFormat:@"%d %.f ", speedCount, [newLocation speed]];

}

+ (NSString *) speedToMPH: (float) value
{
NSString *speedCount = @"0.0 ";
if (value>0) {
float mile = 1609.344f;
float mph = value / mile * 3600;
speedCount = [NSString stringWithFormat: @"%.f ", mph];
}
return speedCount;
}

最佳答案

CLLocation 返回以米/秒为单位的速度。因此,您必须将该值乘以 2.23694,将其转换为英里/小时。

self.speedView.text = [NSString stringWithFormat:@"%d %.f ", speedCount, [newLocation speed] * 2.23694];

关于ios - UIlabel不更新用户当前速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302460/

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