- 아이폰 MapView를 이용한 Google Map을 띄우는 간단한 예제이다.
- 화면 디자인
- MapView 만들기 순서
1. Xcode 실행
2. Single View Application
3. Project Name : “MapViewTest”
4. [TARGETS] - [Build Phases] - [Link Binary With Libraries] - ▶ - +
5. MapKit.framework 와 CoreLocation.framework - [Add]
6. storyboard에 Map View 위치
7. Assistant Editor로 Map View 드래그
8. “myMapView” 입력
9. ViewController.h 파일 편집
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController: UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *lm; }
@property(weak, nonatomic) IBOutlet MKMapView *myMapView;
10. ViewController.m 파일 편집
-(void)viewDidLoad { [super viewDidLoad];
lm = [[CCLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyHundredMeters;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
myMapView.mapType = MKMapTypeStandard;
//myMapView.mapType = MKMapTypeSatelite;
//myMapView.mapType = MKMapTypeHybrid;
}
-(void)locationManager:(CLLocationManager *) manager
didUpdateToLocation:(CLLocation *) newlocation fromLocation:(CLLocation *) oldLocation {
MKCoordinateRegion = myMapView.region;
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[myMapView setRegion:region animated:YES];
}
9. 실행
구글 지도에 가서 검색후 아래 코드를 주소창에 치면 좌표값이 나온다.
* javascript:void(prompt("",gApplication.getMap().getCenter()));
- ""는 단일 인용 부호 두 개
- ViewController.m 소스코드
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myMapVIew;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyHundredMeters;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
myMapVIew.mapType = MKMapTypeStandard;
//myMapVIew.mapType = MKMapTypeSatelite;
//myMapVIew.mapType = MKMapTypeHybrid;
}
- (void)viewDidUnload
{
[self setMyMapVIew:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MKCoordinateRegion region = myMapVIew.region;
region.center.latitude = 35.249474320741; //좌표
region.center.longitude = 128.9023894071579; //좌표
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[myMapVIew setRegion:region animated:YES];
}
@end
- 실행화면
'C/Objective C > ios' 카테고리의 다른 글
아이폰 HtmlParsing 예제 따라하기 (3) | 2012.07.22 |
---|---|
아이폰 WebView 만들기 예제 따라하기 (0) | 2012.07.18 |
아이폰 DatePicker 만들기 예제 따라하기 (0) | 2012.07.18 |
아이폰 Timer를 이용한 Animation 만들기 예제 따라하기 (0) | 2012.07.18 |
아이폰 Animation 만들기 예제 따라하기 (0) | 2012.07.17 |