distanceFromLocation - 获取错误

时间:2010-09-02 09:11:53

标签: iphone core-location

我有以下代码:

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

MyCLLocation.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行上收到以下错误消息“初始化中的不兼容类型”。

有人有什么想法吗?

此致 斯蒂芬

2 个答案:

答案 0 :(得分: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;
    }
}

你能编辑你的问题并显示你在哪里定义了currentlocation和previouslocation - 我在方法中定义了它们只是为了测试它但我假设你已经在你的类中的其他地方定义了它们(可能是.h文件) ?

答案 1 :(得分:0)

问题解决了:

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