0

几天来一直有这个问题,似乎无法找到解决方案。这可能是一些非常基本的东西,但仍然无法提出解决方案。所以我试图让我的 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;
 }
4

1 回答 1

1

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

self.speedView.text = [NSString stringWithFormat:@"%d %.f ", speedCount, [newLocation speed] * 2.23694];
于 2013-08-18T18:59:14.490 回答