2

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.

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

4 Answers4

11

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
  • 21,623
  • 6
  • 63
  • 87
  • I could be wrong, should: NSLog(@"%f %f, %@ ", here.latitude, here.longitude); not be: NSLog(@"%f %f", here.latitude, here.longitude); – fuzzygoat May 20 '10 at 14:11
  • Oh, absolutely right. That will give you a BAD_ACCESS error. Corrected. – Dan Ray May 21 '10 at 12:32
  • Will the update location ticker work even if the app is minimized? – kmehta Jun 14 '11 at 14:57
  • @kmehta - For this to work while the app is backgrounded, you need to set the info.plist key "UIBackgroundModes" to "location". You should expect a very serious hit to battery life if you do this, though! – Dan Ray Jun 14 '11 at 17:15
1

You will have to do the following:

  1. If device cannot access internet
    1. Get co-ordinates from GPS device
    2. Send these co-ordinates via SMS
    3. Receive and decode SMS message at the SMS gateway you have to configure to receive info from device.
    4. Update the info on the application database or any other store you are using
    5. Update the position on map with latest info
  2. If device can access internet
    1. Get co-ordinates from GPS device
    2. Connect to application server (may be some service) and upload information
    3. Update the info on the application database or any other store you are using
    4. Update the position on map with latest info
j0k
  • 22,600
  • 28
  • 79
  • 90
Saurabh
  • 331
  • 4
  • 12
  • 1
    Forget about 1. If you don't have internet you won't get the coordinates out of the device. At least not without the user pressing "Send". iOS does not allow to send SMS without user-interaction. – Matthias Bauch Sep 29 '12 at 22:58
1

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
  • 181,030
  • 38
  • 327
  • 365
1

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
  • 892
  • 5
  • 12