簡體   English   中英

UIlabel不會更新用戶當前速度

[英]UIlabel doesn't update user current speed

已經有幾天這個問題了,似乎無法找到解決方案。 這可能是一些非常基本的東西,但仍然無法提出解決方案。 因此,我試圖讓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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM