IPhone SDK: Track User Location Using GPS

I have some questions about CoreLocation and GPS.

First, what method is used in the main location to constantly obtain the current coordinates of users? And in what period of time should they be received?

Second, should these coordinates be inserted into the NSMutableArray every time they are received, so that the coordinate array will represent the users' path?

Thanks, just want me to start thinking about it.

+2


a source to share


4 answers


A very shorthand version:

First apply the protocol <CLLocationManagerDelegate>

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 method -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

will ping every time Core Location has something to tell you what should happen every few seconds. those CLLocation objects contain precision information, so you can display good points in this method.

Make sure to call [locationManager stopUpdatingLocation]

and then [locationManager release]

at some point!

Good luck to you!

+11


a source


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



+1


a source


You can define which range is appropriate for accuracy, as well as how often you want to receive automatic updates (based on the distance from the last dot machine). Also you can just turn off the location manager and turn it back using some timers.

As far as storing locations to create a path is, it's not that easy. You will continually receive GPS locations at the beginning until the desired accuracy is achieved, and for any points in the future you may get more than one, which is inaccurate before you get a good location. Therefore, making a list of these items will be basically just a list of their paths and also a lot of extra points. You could solve this by keeping only those points that have the desired accuracy, but this is an imperfect world for that matter.

At best, I would suggest you keep two lists, one is the path and the other is the list of running places where you compare until you get the exact location, then put that in your path list. Some of the sample projects do things along these lines, test them.

+1


a source


You will need to do the following:

  • If the device cannot access the Internet
    • Get coordinates from GPS device
    • Send these coordinates via SMS
    • Receive and decode the SMS message on the SMS gateway that you must configure to receive information from the device.
    • Update your app database or whatever store you are using.
    • Refresh map position with the latest information
  • If the device has Internet access
    • Get coordinates from GPS device
    • Connect to the application server (there may be some kind of service) and download information
    • Update your app database or whatever store you are using.
    • Refresh map position with the latest information
+1


a source







All Articles