tags:

views:

39

answers:

1

Hello,

I've been looking at this for a while. I've found plenty of posts and looked at the documentation but I'm having trouble getting it working. I have core location implemented, and its working with no problems i.e.

NSLog(@"Description: %@\n", [newLocation description]);

The description details are displayed in the log.

I want to capture the current location when the user taps on a button and as they start to move recapture their location intermittently. Using this information I want to calculate the distance between the starting point and the current point.

I know I can use the following:

CLLocationDistance distance = [myLocation distanceFromLocation:restaurantLocation]

to calculate the distance but I'm not sure how to capture the current location.

If anyone could post some sample code that would be great. I close to getting this working, just need a final push over the line.

Regards, Stephen

Part 2: Since the original post, I've made a bit of progess. Details posted below:

#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];

    CLLocation *item1 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    CLLocation *item2 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];

    int meters = [item1 getDistanceFrom:item2]; 
    NSLog(@"Distance-d: %d", meters);

    [item1 release];
    [item2 release];


}


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

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

@end

Am I right to calculate the distance here or should I be doing it in NewWorkoutViewController.m, in the locationUpdate method ?

A: 

You set a delegate on your location manager which will be called when the location changes. You can control (indirectly) how often it gets called by specifing a distance filter so that you will only get a callback if the location has moved at least that amount.

progrmr
I've made a bit of progress and implemented some code as you've stated above, but have a few more questions. I've updated my original post (See Part 2).
Stephen