2013-08-18 3 views
0

며칠 동안이 문제가 발생하여 해결책을 찾지 못하는 것 같습니다. 아마 아주 기본적인 것들이지만 여전히 해결책을 찾을 수는 없습니다. 그래서 나는 uilabel을 자동차 또는 자전거로 사용자 속도를 업데이트하려고 노력하고 있습니다. 그러나 UIlabel 업데이트 할 때 올바른 값을 업데이트하지 않습니다. 어떤 도움을 주시면 감사하겠습니다. Heres는 내 .H 파일UIlabel이 현재의 사용자 속도를 업데이트하지 않습니다

#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; 
} 
+0

어떻게 올바른 값이 아닌지 알고 계십니까? 표시된 값은 무엇이고 볼 가치가있는 값은 무엇입니까? –

+0

그래서 운전하면서 전화로 시험해 보았습니다. 그래서 나는 30mph로 갈 것이고 myuilabel에서 나는 17.3에 운전하고 있다고 말할 것입니다. 어떤 아이디어 왜 – mrorgon

+0

@mrios 귀하의 질문은 xcode 또는 아이폰과 아무 상관이 없습니다. 이것이 태그가 제거 된 이유입니다. – rmaddy

답변

1

CLLocation 초당 미터 속도를 반환합니다. 따라서 2.23694 값을 곱하여 시간당 마일로 변환해야합니다.

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

다양한 로케일의 사용자가 예상대로 나타나도록 숫자의 형식을 올바르게 지정하려면 NSNumberFormatter를 사용해야합니다. – rmaddy

+0

도움을 주셔서 감사합니다. – mrorgon

관련 문제