IanMK2 Blog

현재 위치와 어느한지점을 지도에 뿌리기위해 맵뷰를 쓸일이 생겼다.

사전작업은 다음과 같다.
프레임워크에서 CoreLocation.framework를 추가
헤더는 MapKit/MapKit.h를 추가
일단 원하는 위치에 핀을 박기위한 클래스
///////////////////PlaceMark.h//////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PlaceMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end


//////////////////PlaceMark.m/////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface PlaceMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end



실제 사용소스시
        MKMapView *gmap = [[MKMapView alloc] initWithFrame:self.view.bounds];
gmap.showsUserLocation = TRUE; //자기위치 표시
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;//표시되는 영역의 설정 수치가 작을수록 더 확대된다.
span.longitudeDelta= 0.005;
CLLocationCoordinate2D location = gmap.userLocation.coordinate;
location.latitude=[[storedata objectForKey:@"latitude"] floatValue];//GPS좌표경위도설정
location.longitude=[[storedata objectForKey:@"longitude"] floatValue];
region.span = span;
region.center = location;
[gmap setRegion:region animated:TRUE];
[gmap regionThatFits:region];
PlaceMark *placemark = [[PlaceMark alloc] initWithCoordinate:location]; //핀박을위치설정
placemark.title = @"Title"
placemark.subtitle = @"SubTitle";

[gmap addAnnotation:placemark];  //핀을 박아넣는다.
[self.view addSubview:gmap]; //뷰에 추가

[placemark release];
[gmap release];


Posted by IanMK2