Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find current location of device (using CoreLocation framework)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 521
    Comment on it

    There are two ways to find current location (lat,long) of device : Using CoreLocation framework OR using Mapkit framework.

    Here's how to get the location using CoreLocation framework :


    1. Import CoreLocation/CoreLocation.h in h file of view controller.
    2. Create a global CLLocationManager object in the class.

    #import <CoreLocation/CoreLocation.h>
    @interface LocationViewController : NSObject <CLLocationManagerDelegate> {
        CLLocationManager *locationManager;
    }
    


    3. Instantiate the object in viewDidLoad method or anywhere where you initialize other objects.

    locationManager = [[CLLocationManager alloc] 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    


    4. Implement CLLocationDelegate methods

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation *newLocation = locations[0];
        NSLog(@"lat: %f lon: %f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
    }
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        NSLoc(@"Error %@",error);
    }
    

    According to apple documentation :

    desiredAccuracy is for the accuracy of result location data. The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed. Following are options that can be used as per requirement :
    kCLLocationAccuracyBestForNavigation
    kCLLocationAccuracyBest // this is the default value
    kCLLocationAccuracyNearestTenMeters
    kCLLocationAccuracyHundredMeters
    kCLLocationAccuracyKilometer
    kCLLocationAccuracyThreeKilometers
    Determining a location with greater accuracy requires more time and more power. Hence the value which is appropriate for the required scenario should be used.

    distanceFilter is the minimum distance (measured in meters) a device must move horizontally before an update event is generated. This distance is measured relative to the previously delivered location. Use the value kCLDistanceFilterNone to be notified of all movements. The default value of this property is kCLDistanceFilterNone.

    CLLocation object contains geographical coordinates and altitude of the devices location along with values indicating the accuracy of the measurements and the time when those measurements were made. In iOS, this class also reports information about the speed and heading in which the device is moving.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: