Develop
2014.05.24 20:19
[ios] 아이폰 GPS 사용하기
조회 수 4049 댓글 0
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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
585 | Develop | [ios] 비디오,네트워크,소셜로그인 테스트 | hooni | 2017.04.04 | 805 |
584 | Develop | [ios] 비동기 블럭 코드 예제 | hooni | 2014.11.21 | 904 |
583 | Develop | [ios] 배열(NSArray) 연산과 간단한 애니메이션(split images) | hooni | 2013.10.31 | 45585 |
582 | Develop | [ios] 미스터피자(Mr.pizza) 어플 | hooni | 2013.04.23 | 42695 |
581 | Develop | [ios] 문자열로 함수 실행하기 (eval 함수처럼) | hooni | 2015.02.10 | 932 |
580 | Develop | [ios] 로컬에 있는 JS 파일 웹뷰에서 동적으로 실행하기 | hooni | 2015.02.10 | 1078 |
579 | Develop | [ios] 로컬에 있는 html 실행하기 | hooni | 2015.02.10 | 1144 |
578 | Develop | [ios] 디렉토리 하하하.. | hooni | 2013.04.23 | 32661 |
577 | Develop | [ios] 동영상 플레이어 샘플 (for Remote Url) | hooni | 2017.02.07 | 1765 |
576 | Develop | [ios] 동영상 플레이어 샘플 (for PIP Player) | hooni | 2017.03.15 | 1406 |
575 | Develop | [ios] 동영상 플레이어 샘플 (for Local File) | hooni | 2017.02.07 | 1208 |
574 | Develop | [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동 | hooni | 2014.01.24 | 12523 |