▼iPhoneプログラミングメモ▼
マップビュー

マップを表示するプログラムを作成する。


フレームワークの準備
MapKit.frameworkを追加。

ソースコードの記述
MapViewEx.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

//MapViewExの宣言
@interface MapViewEx : UIViewController <MKMapViewDelegate> {
    MKMapView* _mapView;
}
@end

MapViewEx.m
#import "MapViewEx.h"

//MapViewExの実装
@implementation MapViewEx

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];
        
    //マップビューの生成
    _mapView=[[MKMapView alloc] init];
    [_mapView setFrame:self.view.frame];
    [_mapView setMapType:MKMapTypeStandard];
    [_mapView setDelegate:self];
    [self.view addSubview:_mapView];

    //ビューサイズの自動調整
    _mapView.autoresizingMask= 
         UIViewAutoresizingFlexibleRightMargin| 
         UIViewAutoresizingFlexibleTopMargin|
         UIViewAutoresizingFlexibleLeftMargin|
         UIViewAutoresizingFlexibleBottomMargin|
         UIViewAutoresizingFlexibleWidth|
         UIViewAutoresizingFlexibleHeight;

    //位置の値の用意
    CLLocationCoordinate2D coordinate;
    coordinate.latitude =35.707527;
    coordinate.longitude=139.760857;

    //ズームの値の用意
    MKCoordinateSpan span;
    span.latitudeDelta =0.01;
    span.longitudeDelta=0.01;
  
    //ズームと位置の指定
    MKCoordinateRegion region;
    region.center=coordinate;
    region.span  =span;
    [_mapView setRegion:region animated:YES];
}

//メモリ解放
- (void)dealloc {
    [_mapView release];
    [super dealloc];
}

//マップロード成功時に呼ばれる
- (void)mapViewDidFinishLoadingMap:(MKMapView*)mapView {
    NSLog(@"マップロード成功");
}

//マップロード失敗時に呼ばれる
- (void)mapViewDidFailLoadingMap:(MKMapView*)mapView 
    withError:(NSError*)error {
    NSLog(@"マップロード失敗");   
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end



−戻る−