2010-09-02 40 views
0

我已經制定了下面的代碼:distanceFromLocation - 收到錯誤

NewWorkoutViewController.h

#import <UIKit/UIKit.h> 
//#import <CoreLocation/CoreLocation.h> 
#import <CoreData/CoreData.h> 

#import "MapViewController.h" 
#import "StatisticsViewController.h" 
#import "MyCLController.h" 
#import "Workout.h" 
#import "Route.h" 

@interface NewWorkoutViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate, UIAlertViewDelegate> { 
    MyCLController *locationController; 

    IBOutlet UIButton *saveButton; 
    IBOutlet UIButton *backButton; 

    IBOutlet UIButton *startButton; 
    IBOutlet UIButton *stopButton; 
    IBOutlet UIButton *resetButton; 

    IBOutlet MapViewController *mapViewController; 
    IBOutlet StatisticsViewController *statisticsViewController; 

    IBOutlet UIView *routePickerView; 
    IBOutlet UIPickerView *routePicker; 

    IBOutlet UIView *activityPickerView; 
    IBOutlet UIPickerView *activityPicker; 

    IBOutlet UIView *intensityPickerView; 
    IBOutlet UIPickerView *intensityPicker; 

    IBOutlet UILabel *time; 
    IBOutlet UITextField *route; 
    IBOutlet UITextField *activity; 
    IBOutlet UITextField *intensity; 
    IBOutlet UILabel *speed; 
    IBOutlet UILabel *distance; 
    IBOutlet UILabel *averageSpeed; 
    IBOutlet UILabel *calories; 

    NSMutableArray *routeArray; 
    NSMutableArray *activityArray; 
    NSMutableArray *intensityArray; 

    NSMutableArray *newWorkoutArray; 

    NSManagedObjectContext *managedObjectContext; 

    int counterInt; 
    NSTimer *myTimer; 
    NSInteger *startInterval; 
    NSInteger *stopInterval; 
    NSInteger *elapsedInterval; 

    NSString *mapID; 
    int pickerChoice; 

    NSString *walkID; 
    NSString *activityValue; 
    NSString *intensityValue; 

    CLLocation *currentlocation; 
    CLLocation *previouslocation; 

    //double kilometers; 
    //double totalkilometers; 
} 

@property (retain,nonatomic) NSMutableArray *newWorkoutArray; 
@property (retain,nonatomic) NSTimer *myTimer; 
@property (nonatomic,assign) NSManagedObjectContext *managedObjectContext; 

-(IBAction)backButton; 
-(IBAction)saveButton; 
-(IBAction)mapButton; 
-(IBAction)statisticsButton; 
-(IBAction)startTimerButton; 
-(IBAction)stopTimerButton; 
-(IBAction)resetButton; 

-(IBAction)routePickerShow; 
-(IBAction)activityPickerShow; 
-(IBAction)intensityPickerShow; 

-(IBAction)routeDoneButton; 
-(IBAction)activityDoneButton; 
-(IBAction)intensityDoneButton; 

-(void)showActivity; 
-(void)didCreateWorkout:(NSString *)thisTime 
        Route:(NSString *)thisRoute 
       Activity:(NSString *)thisActivity 
       Intensity:(NSString *)thisIntensity 
        Speed:(NSString *)thisSpeed 
       Distance:(NSString *)thisDistance 
      AverageSpeed:(NSString *)thisAverageSpeed 
       Calories:(NSString *)thisCalories; 

-(void)initialiseWorkoutViewController; 
-(void)locationUpdate:(CLLocation *)location; 


@end 

NewWorkoutViewController.m

-(void)locationUpdate:(CLLocation *)location 
{ 
    currentlocation = [[CLLocation alloc] initWithLatitude:+37.364305 longitude:-122.027901]; 
    previouslocation = [[CLLocation alloc] initWithLatitude:+37.364429 longitude:-122.028048]; //70301 

    if(previouslocation != nil) {   
     CLLocationDistance kilometers = [currentlocation distanceFromLocation:previouslocation]; // Error occurring here 

     NSLog(@"Distance Meters: %f", kilometers); 

     [speed setText:[NSString stringWithFormat:@"%.2f",[location speed]]]; 
     [distance setText:[NSString stringWithFormat:@"%.2f", kilometers/1000]]; 

     previouslocation = currentlocation; 
    } 
} 

MyCLLocation.h

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


@protocol MyCLControllerDelegate <NSObject> 
@required 
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error; 
@end 

@interface MyCLController : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
    id delegate; 
} 

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, assign) id delegate; 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation; 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error; 

@end 

MyCLLoc ation.m

#import "MyCLController.h" 


@implementation MyCLController 

@synthesize locationManager; 
@synthesize delegate; 

- (id) init { 
    self = [super init]; 
    if (self != nil) { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; // send loc updates to myself 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    [self.delegate locationUpdate:newLocation]; 
} 


- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
    [self.delegate locationError:error]; 
} 


- (void)dealloc { 
    [self.locationManager release]; 
    [super dealloc]; 
} 

@end 

我得到的NewWorkoutViewController.m「不兼容類型初始化」的CLLocationDistance線以下錯誤消息。

任何任何想法?

問候, 斯蒂芬

+0

我知道這並不能幫助你的問題,但它是非常誤導的調用該變量公里 - 該distanceFromLocation方法返回米) – deanWombourne 2010-09-02 09:47:53

+0

謝謝院長,無論你相信與否其實我有按照原來的代碼行: CLLocationDistance公里+ = [當前位置distanceFromLocation:previouslocation]/1000; 因此公里而不是米的原因。 – Stephen 2010-09-02 10:00:56

回答

0

問題解決了:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 3130 
      CLLocationDistance kilometers = [currentLocation getDistanceFrom:previousLocation]/1000; 
     #else 
      CLLocationDistance kilometers = [currentLocation distanceFromLocation:previousLocation]/1000;   
     #endif 
0

我已經試過了XCode中,但沒有得到你得到:(

我試過錯誤:

-(void)locationUpdate:(CLLocation *)location 
{ 
    CLLocation *currentlocation = [[CLLocation alloc] initWithLatitude:+37.364305 longitude:-122.027901]; 
    CLLocation *previouslocation = [[CLLocation alloc] initWithLatitude:+37.364429 longitude:-122.028048]; //70301 

    if(previouslocation != nil) {   
     CLLocationDistance kilometers = [currentlocation distanceFromLocation:previouslocation]; // Error occurring here 

     NSLog(@"Distance Meters: %f", kilometers); 

     [speed setText:[NSString stringWithFormat:@"%.2f",[location speed]]]; 
     [distance setText:[NSString stringWithFormat:@"%.2f", kilometers/1000]]; 

     previouslocation = currentlocation; 
    } 
} 

您可以編輯您的問題並顯示你已經定義了當前位置和先前位置的位置 - 我已經在方法中定義了它們來測試它,但我認爲你已經將它們定義在類中的其他位置(可能是.h文件)?

+0

我已根據要求包含.h。 – Stephen 2010-09-02 10:07:49

+0

Dean,蘋果文檔引用了以下代碼行: - (CLLocationDistance)distanceFromLocation:(const CLLocation *)的位置, 使用distanceFromLocation時。我覺得有點愚蠢的問這個問題,但我需要嗎,我猜我是嗎? – Stephen 2010-09-02 10:14:36

+0

嗯,你.h文件看起來完全如我所料 - CLLocation *爲currentlocation和previouslocation。我假設你也在.h文件的頂部有一行_#import _? – deanWombourne 2010-09-02 10:43:16