views:

789

answers:

3

I have a few questions about CoreLocation and GPS.

First, what method in core location is used to continually get the users current coordinates? And at what interval should these be retrieved?

Second, should these coordinates be pushed into a NSMutableArray each time they are received, so that the array of coordinates will represent the users path?

Thanks, just wanting to get started getting me mind around this.

+1  A: 

The best way to is read the CLLocationManager Class Reference, which links to several example projects. The short version:

  1. Set the delegate property to a class that will receive location updates.
  2. Implement the CLLocationManagerDelegate protocol in the delegate.
  3. Call the appopriate methods to start updating location and/or heading.
Marcelo Cantos
+1  A: 

You are able to define what range is acceptable for accuracy as well as how often you wish to receive automatic updates (based on a distance from last point mechanism). Also you can just turn off the location manager and turn it back on at will thru some use of a timer.

As for saving the locations to build a path, its not that simple. You will continually get GPS locations at first until the desired accuracy is achieved, and for any points in the future you may get more than one that is inaccurate before you get a good location. So building a list of these points will basically just be a list of their path, along with a lot of extra points. You could solve this by saving only those points that have the accuracy you desire, but its an imperfect world in this respect.

Best case I would suggest you keep two lists, one is the path and the other is a running list of locations where you are comparing until you get a highly accurate location, then putting that on your path list. Some of the example projects do things along these lines, do check them out.

EricLeaf
+1  A: 

A very abbreviated version:

First, adopt the <CLLocationManagerDelegate> protocol in your .h, and #import <CoreLocation/CoreLocation.h>.

Then in .m go:

- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}


-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    CLLocationCoordinate2D here =  newLocation.coordinate;
    NSLog(@"%f  %f ", here.latitude, here.longitude);
}

Your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method will get pinged every time Core Location has something to say to you, which should happen every few seconds. those CLLocation objects contain info about accuracy, so you can screen for good points in that method.

Be sure to call [locationManager stopUpdatingLocation] and then [locationManager release] at some point!

Good luck finding yourself!

Dan Ray
I could be wrong, should: NSLog(@"%f %f, %@ ", here.latitude, here.longitude); not be: NSLog(@"%f %f", here.latitude, here.longitude);
fuzzygoat
Oh, absolutely right. That will give you a BAD_ACCESS error. Corrected.
Dan Ray