Develop
2014.05.24 20:19

[ios] 아이폰 GPS 사용하기

Views 3943 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
GPS 사용을 위해서 CoreLocation.framework 프레임워크를 추가하고 아래 코드처럼..

# LocationServiceViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface LocationServiceViewController : UIViewController
    <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    IBOutlet UILabel *latitude;
    IBOutlet UILabel *longitude;
    IBOutlet UILabel *Heading;
}

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet UILabel *latitude;
@property (nonatomic, retain) IBOutlet UILabel *longitude;
@property (nonatomic, retain) IBOutlet UILabel *Heading;

@end

# LocationServiceViewController.m
#import "LocationServiceViewController.h"

@implementation LocationServiceViewController

@synthesize locationManager;
@synthesize latitude;
@synthesize longitude;
@synthesize Heading;

// Implement viewDidLoad to do additional setup after loading the view,
// typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy =
        kCLLocationAccuracyNearestTenMeters;

    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    [self.locationManager startUpdatingHeading];
    
    CLLocation *curPos = self.locationManager.location;

    // 위도
    NSNumber *nLatitude =
        [NSNumber numberWithDouble:curPos.coordinate.latitude];

    // 경도
    NSNumber *nLongitude =
        [NSNumber numberWithDouble:curPos.coordinate.longitude];
    
    NSString *sPosition1 =
        [NSString stringWithFormat:@"%.3f", [nLatitude doubleValue]];

    NSString *sPosition2 =
        [NSString stringWithFormat:@"%.3f", [nLongitude doubleValue]];

    //NSLog(@"%@",sPosition);
    [self.latitude setText:sPosition1];
    [self.longitude setText:sPosition2];
}

// 좌표 업데이트 델리게이트
-(void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
{
    // GPS 좌표 업데이트 된 후 호출 됨
    
    // 위도
    NSNumber *nLatitude =
        [NSNumber numberWithDouble:newLocation.coordinate.latitude];

    // 경도
    NSNumber *nLongitude =
        [NSNumber numberWithDouble:newLocation.coordinate.longitude];
    
    NSString *sPosition1 =
        [NSString stringWithFormat:@"%.3f", [nLatitude doubleValue]];

    NSString *sPosition2 =
        [NSString stringWithFormat:@"%.3f", [nLongitude doubleValue]];
    //NSLog(@"%@",sPosition);
    [self.latitude setText:sPosition1];
    [self.longitude setText:sPosition2];
}

// 나침반 델리게이트
-(void)locationManager:(CLLocationManager *)manager
    didUpdateHeading:(CLHeading *)newHeading
{
    NSString *headingStr =
        [NSString stringWithFormat:@"%.4lf", newHeading.magneticHeading];

    //NSLog(@"%@",headingStr);
    [self.Heading setText:headingStr];
}

// GPS 오류 델리게이트
-(void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
{
    // GPS 좌표 업데이트에 오류가 발생할 때 호출 됨
}

// Override to allow orientations
// other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.locationManager stopUpdatingHeading];
    self.locationManager = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end

?

List of Articles
No. Category Subject Author Date Views
697 Develop [c++] 인라인 함수 설명과 예제.. file hooni 2013.04.23 6633
696 Develop [c++] 인라인 함수에 대한 설명 hooni 2013.04.23 7110
695 Develop [c++] 자료구조(링크리스트,스택,큐)와 후위 표기 계산기 샘플 ㅋㅋ 4 file hooni 2013.04.23 12318
694 Develop [c++] 중복실행 방지(Mutex;뮤텍스 ) 샘플 소스.. ㅋㅋ file hooni 2013.04.23 8737
693 Develop [c++] 채팅로봇 소스.. ㅋㄷㅋㄷ file hooni 2013.04.23 8878
692 Develop [c++] 초간단 스택 두 가지 방식(class, struct) file hooni 2013.04.23 7553
691 Develop [c++] 템플릿(Template) 예제 소스.. file hooni 2013.04.23 6976
690 Develop [c++] 트리컨트롤 스텝 2 예제.. file hooni 2013.04.23 6953
689 Develop [c++] 트리컨트롤 스텝 3 예제.. file hooni 2013.04.23 7943
688 Develop [c++] 트리컨트롤 예제1 ㅋㅋ file hooni 2013.04.23 7169
687 Develop [c++] 퍼즐 버블버블 간단한 원리(사용공식) file hooni 2013.04.23 9958
686 Develop [c++] 프리렉(freelec) 예제 자료.. ㅋㄷ file hooni 2013.04.23 6833
685 Develop [c++] 헤더, 소스 파일 분리해서 작성해본 테스트 소스 file hooni 2013.04.23 6688
684 Develop [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ file hooni 2013.04.23 12064
683 Develop [c++] 현승이가 준 메신저 소스.. ㅋㅋ file hooni 2013.04.23 8264
682 Develop [c++][mfc] 파일 입출력 샘플 (한줄씩 읽어서 다른 파일에 쓰기) hooni 2013.04.23 14979
Board Pagination Prev 1 ... 8 9 10 11 12 ... 53 Next
/ 53