views:

261

answers:

2

Hello,

I have the following code in place:

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

I get the following error message on the CLLocationDistance line in NewWorkoutViewController.m "Incompatible types in initialization".

Anyone any thoughts ?

Regards, Stephen

A: 

I've tried it in XCode but don't get the error that you get :(

I tried :

-(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;
    }
}

Can you edit your question and show where you have defined currentlocation and previouslocation - I've defined them in the method just to test it but I assume you've defined them somewhere else in your class (probably the .h file)?

deanWombourne
I've included the .h as requested.
Stephen
Dean, the apple documentation refers to the the following line of code: -(CLLocationDistance)distanceFromLocation:(const CLLocation *)location, when using distanceFromLocation. I feel a bit stupid asking this but do I need it, I'm guessing I do ?
Stephen
Hmm, you .h file looks exactly as I expected - CLLocation * for both currentlocation and previouslocation. I assume you also have the line _#import <CoreLocation/CoreLocation.h>_ at the top of the .h file?
deanWombourne
yes, I'm importing <CoreLocation/CoreLocation.h>.
Stephen
In that case I have no idea why you would get a compiler error there - it makes no sense - the method returns a CLLocationDistance and that's what you've declared kilometres as. And I don't get the error when I compile the code on my machine. The only other suggestion I have is to do a clean and rebuild from scratch (Build -> Clean all Targets)
deanWombourne
Thanks for your help Dean, I'll keep at it.
Stephen
Folks, I'm still struggling with this, all help appreciated.
Stephen
A: 

Problem solved:

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