Skip to content

CLLocationManager

CLLocationManager 클래스의 사용방법에 대한 정리.

GPS 위치정보 취득방법

CLLocationManager 클래스의 객체 초기화 방법.

// CLLocationManagerDelegate를 적용한 클래스의 초기화 메서드 내부.
m_locationManager = [[CLLocationManager alloc] init];
m_locationManager.delegate = self;
[m_locationManager startUpdatingLocation];

GPS 경위도좌표 취득 방법.

- (void) locationManager: (CLLocationManager*) manager
     didUpdateToLocation: (CLLocation*) newLocation
            fromLocation: (CLLocation*) oldLocation
{
    if (newLocation != nil){
        _x = newLocation.coordinate.longitude;
        _y = newLocation.coordinate.latitude;
    }
}

Geo-Magnetic Sensor 사용방법

Getting Heading-Related Events. The steps for receiving heading events are as follows:

  1. Create a CLLocationManager object.
  2. Determine whether heading events are available by calling the headingAvailable class method. (In iOS 3.x and earlier, check the value of the headingAvailable property instead.)
  3. Assign a delegate to the location manager object.
  4. If you want true north values, start location services.
  5. Call the startUpdatingHeading method to begin the delivery of heading events.

Initiating the delivery of heading events.

- (void)startHeadingEvents {
    if (!self.locManager) {
        CLLocationManager* theManager = [[[CLLocationManager alloc] init] autorelease];
        // Retain the object in a property.
        self.locManager = theManager;
        locManager.delegate = self;
    }

    // Start location services to get the true heading.
    locManager.distanceFilter = 1000;
    locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [locManager startUpdatingLocation];

    // Start heading updates.
    if ([CLLocationManager headingAvailable]) {
        locManager.headingFilter = 5;
        [locManager startUpdatingHeading];
    }
}

CLLocationManagerDelegate를 사용하여 정상적인 데이터를 받을 경우 locationManager:didUpdateHeading: 메서드에 통보된다. 유효성 확인을 위한여 headingAccuracy를 사용한다.

Processing heading events:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    if (newHeading.headingAccuracy < 0)
        return;

    // Use the true heading if it is valid.
    CLLocationDirection theHeading = ((newHeading.trueHeading > 0) ?
    newHeading.trueHeading : newHeading.magneticHeading);
    self.currentHeading = theHeading;
    [self updateHeadingDisplays];
}

See also

Favorite site