아이폰 어플중에서 지도에 자기 좌표가 찍히고 주소가 나오는 어플들을 많이들 보셨을 텐데요.
오늘은 좌표값과 주소 구하는 방법에 대해서 알아보겠습니다.
이 기능은 참 많은 어플들을 통해서 활용이 가능할 것 같습니다.
우선 작업목표를 정해보았습니다.
1. 새 프로젝트 (Window-based Application)를 생성한다.
2. 새 프로젝트에 뷰를 하나 추가한다.
3. 추가한 뷰에 검색된 결과를 보여줄 화면(Label,Button등등)을 구성한다.
3. 현재 좌표값을 구한다.
4. 구해진 좌표값을 화면에 뿌린다.
5. 구해진 좌표값에 해당하는 주소를 구글맵에서 받아온다.
6. 받아온 결과를 화면에 뿌려준다.
막상 써 놓으니 음... 생각보단 간단하네요~!!
자~~ 이제 목표는 정해졌으니 하나씩 해보면 됩니다.ㅋ
이제 작업목표에서 정한 순서로 진행과정을 살펴보겠습니다..
시작해 볼까요~
XCode에서 File->New Project->Window-based Application을 클릭하세요.
(아시는 분들도 많겠지만 혹시 모르는 분도 있을까 해서 아래 이미지 참조)
2. 새 프로젝트를 윈도우 베이스로 만들었지만 뭔가 보여줄 화면이 필요합니다.
화면용 파일을 하나 프로젝트에 추가합니다.
XCode화면의 Groups&Files 영역에서 마우스 오른쪽 클릭해서 Add->New File을 합니다.
3. 이제 화면도 만들었습니다.
Command+R 키로 그냥 한번 실행해봅니다. 아무것도 없습니다..
화면에 뭔가 만들어주어야 합니다. 다른 것을 사용해도 되겠지만 이번 작업에서는 단순하게 Label로 모두 처리하도록 합니다.
아래화면 참고해서 같이 만들어보죠.
참고로 화면에 구성된 Label들은 소스파일의 IBOutlet로 선언된 변수들과 연결해주어야 합니다.
4. Label과 아웃렛과의 연결 하셨다면 이제 좌표값을 구해보겠습니다.
좌표값을 구하기 위해서는 CoreLocation.framework을 현재 프로젝트에 추가해주어야 합니다.
추가했나요? 이젠 좌표구하는 부분 소스를 보겠습니다.
/*
viewDidLoad 이벤트에 CLLocationManager를 생성해서 실행줍니다.
그러면 #pragma mark - 이하 부분이 자동적으로 실행됩니다.
핵심 소스가 여기입니다.
*/
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startingPoint == nil)
self.startingPoint = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"위도 : %g°", newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
[latitudeString release];
NSString *longitudeString = [[NSString alloc] initWithFormat:@"경도 : %g°", newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
[longitudeString release];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error gettingg location from Core Location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
[alert release];
}
==============================================================================================
5. 좌표값을 받아서 구글맵에서 주소를 조회하는 부분을 보겠습니다.
1) 구글맵에 주소 조회 쿼리를 보내서 결과(XML)를 받아오는 기능을 구현합니다.
2) 받아온 결과(XML)을 파싱하는 기능을 구현합니다.
위에 1), 2)는 유기적으로 함께 구동됩니다.
==================구글맵에 주소 조회 쿼리 보내기=========================
-(IBAction)loadXMLData{
self.xmlUrl = @"http://maps.google.com/maps/api/geocode/xml?latlng=37.630478,127.090199&language=kr&sensor=true";
xmlConnection = [[NSURLConnection alloc]
initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:xmlUrl]]
delegate:self];
if (xmlConnection == nil)
NSLog(@"Connect error");
else
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
xmlParseData = [[NSMutableArray alloc] init];
xmlValue = [[NSMutableString alloc] init];
currectItem = [[NSMutableDictionary alloc] init];
receiveData = [[NSMutableData alloc] init];
}
#pragma mark URLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Receive: %@, %@, %d",
[response URL],
[response MIMEType],
[response expectedContentLength]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@", [error localizedDescription]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSString *str = [[NSString alloc] initWithData:data encoding:0x80000000 + kCFStringEncodingDOSKorean];
//NSData *data1 = [str dataUsingEncoding:NSUTF8StringEncoding];
[receiveData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSString *str = [[NSString alloc] initWithData:receiveData encoding:0x80000000 + kCFStringEncodingDOSKorean];
//NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:receiveData];
[parser setDelegate:self];
[parser parse];
[parser release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[xmlConnection release];
[receiveData release];
}
================================조회 결과 XML을 파싱하여 화면에 보여주기================================
#pragma mark XMLParse delegate methods
//#pragma mark NSXMLParser delegate methods
- (void)parserDidEndDocument:(NSXMLParser *)parser {
self.clickBtn;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"address_component"])
elementType = etItem;
[xmlValue setString:@""];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (elementType != etItem)
return;
if ([elementName isEqualToString:@"long_name"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"short_name"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"address_component"]) {
[xmlParseData addObject:[NSDictionary dictionaryWithDictionary:currectItem]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (elementType == etItem) {
[xmlValue appendString:string];
}
}
좀 부족했지만 위 설명을 잘 수행한다면 다음과 같은 화면이 나오게 됩니다...(제가 사는곳입니다. ㅎㅎㅎ)
부족한 부분은 첨부된 소스 보면 이해가 될겁니다. 파일첨부합니다~!! 화이팅하세요~
'아이폰 개발 이야기' 카테고리의 다른 글
| 아이폰 개발, 배경이미지를 만들어봅시다! (0) | 2010/08/10 |
|---|---|
| 아이폰 개발, 좌표값과 주소 구하기 (7) | 2010/06/29 |
| 아이폰 개발시 유용한 탭바 커스텀(Custom Tab Bar) 구현하기 (12) | 2010/06/25 |
| [아이폰 컨트롤러 예제] ① 테이블뷰 (0) | 2010/05/27 |
| 아이폰 개발자 등록 방법 A TO Z (22) | 2010/05/24 |
| 아이폰 개발 문자열(NSString) 다루기 예제 (0) | 2010/05/10 |




FindArea.zip