Ios-accessing-maps

提供:Dev Guides
移動先:案内検索

iOS-マップへのアクセス

地図は場所を見つけるのに役立ちます。 マップは、MapKitフレームワークを使用してiOSに統合されています。

関与するステップ

  • ステップ1 *-シンプルなビューベースのアプリケーションを作成します。
  • ステップ2 *-プロジェクトファイルを選択し、ターゲットを選択してからMapKit.frameworkを追加します。
  • ステップ3 *-Corelocation.frameworkも追加する必要があります。
  • ステップ4 *-MapViewをViewController.xibに追加し、ibOutletを作成して、mapViewという名前を付けます。
  • ステップ5 *-[ファイル]→[新規]→[ファイル…​]を選択して、新しいファイルを作成します →Objective Cクラスを選択して、[次へ]をクリックします。
  • ステップ6 *-NSObjectとして「サブクラス」でMapAnnotationとしてクラスに名前を付けます。
  • ステップ7 *-作成を選択します。
  • ステップ8 *-MapAnnotation.hを次のように更新します-
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject<MKAnnotation>
@property (nonatomic, strong) NSString *title;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)title andCoordinate:
   (CLLocationCoordinate2D)coordinate2d;

@end

ステップ9 *- *MapAnnotation.m を次のように更新します-

#import "MapAnnotation.h"

@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
   (CLLocationCoordinate2D)coordinate2d {

   self.title = title;
   self.coordinate =coordinate2d;
   return self;
}
@end

ステップ10 *- *ViewController.h を次のように更新します-

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<MKMapViewDelegate> {
   MKMapView *mapView;
}
@end

ステップ11 *- *ViewController.m を次のように更新します-

#import "ViewController.h"
#import "MapAnnotation.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   mapView = [[MKMapView alloc]initWithFrame:
   CGRectMake(10, 100, 300, 300)];
   mapView.delegate = self;
   mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
   mapView.mapType = MKMapTypeHybrid;
   CLLocationCoordinate2D location;
   location.latitude = (double) 37.332768;
   location.longitude = (double) -122.030039;

  //Add the annotation to our map view
   MapAnnotation *newAnnotation = [[MapAnnotation alloc]
   initWithTitle:@"Apple Head quaters" andCoordinate:location];
   [mapView addAnnotation:newAnnotation];
   CLLocationCoordinate2D location2;
   location2.latitude = (double) 37.35239;
   location2.longitude = (double) -122.025919;
   MapAnnotation *newAnnotation2 = [[MapAnnotation alloc]
   initWithTitle:@"Test annotation" andCoordinate:location2];
   [mapView addAnnotation:newAnnotation2];
   [self.view addSubview:mapView];
}

//When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
   MKAnnotationView *annotationView = [views objectAtIndex:0];
   id <MKAnnotation> mp = [annotationView annotation];
   MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance
   ([mp coordinate], 1500, 1500);
   [mv setRegion:region animated:YES];
   [mv selectAnnotation:mp animated:YES];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
  //Dispose of any resources that can be recreated.
}
@end

出力

アプリケーションを実行すると、次のように出力が得られます-

iOSチュートリアル

マップを上にスクロールすると、次のように出力が得られます-

iOSチュートリアル