Add geolocation to your app

walden systems, objective c, geolocation, ios, CLLocationManager, desiredAccuracy, distanceFilter, startUpdatingLocation, stopUpdatingLocation, delegate
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

n this example, we will use standard location service to get geolocation. To use the standard location service, create an instance of the CLLocationManager class and configure its desiredAccuracy and distanceFilter properties. To begin receiving location notifications, assign a delegate to the object and call the startUpdatingLocation method. As location data becomes available, the location manager notifies its assigned delegate object. If a location update has already been delivered, you can also get the most recent location data directly from the CLLocationManager object without waiting for a new event to be delivered. To stop the delivery of location updates, call the stopUpdatingLocation method of the location manager object.

In this code, we will instantiate a CLLocationManager object ( if one doesn't exist already ), set the desiredAccuracy to km, set the distanceFilter to get the minimum distance a device must move before an update event is generated, set a delegate, and start updating.

- (void)startStandardUpdates
{
     int l_changeInMeters ;

     l_changeInMeters = 100 ;
     if (nil == locationManager)
     {
          locationManager = [[CLLocationManager alloc] init];
     }
     locationManager.delegate = self;
     locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
     locationManager.distanceFilter = l_changeInMeters;
     [locationManager startUpdatingLocation];
}


This is the delegate method for receiving location events. Because the location manager object sometimes returns cached events, we check the timestamp of any location events you receive. In this example, the method throws away any events that are more than fifteen seconds old because we assume that events up to that age are good enough.

- (void)locationManager:(CLLocationManager *)currentLocationManager
     didUpdateLocations:(NSArray *)currentLocationList
{
     CLLocation* newLocation = [currentLocationList lastObject];
     NSDate* eventDate = newLocation.timestamp;
     NSTimeInterval timeThreshold = [eventDate timeIntervalSinceNow];
     if (abs(timeThreshold) < 15.0)
     {
          NSLog(@"latitude %+.6f, longitude %+.6f ",
          newLocation.coordinate.latitude,
          newLocation.coordinate.longitude);
     }
}