Objective-C (iOS): 標準 Map

MapViewController

Objective-C: MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"

@interface MapViewController : UIViewController

@end

Objective-C: MapViewController.m

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupMap];
}

- (void)setupMap
{
    MKMapView *mapView = [[MKMapView alloc] init];
    mapView.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
    
    // 中心点
    CLLocationCoordinate2D appleStoreGinzaCoord;
    appleStoreGinzaCoord.latitude = 35.672303; // 経度
    appleStoreGinzaCoord.longitude = 139.76572; // 緯度
    [mapView setCenterCoordinate:appleStoreGinzaCoord animated:NO];
    
    // 縮尺
    MKCoordinateRegion mapScale = mapView.region;
    mapScale.center = appleStoreGinzaCoord;
    mapScale.span.latitudeDelta = mapScale.span.longitudeDelta = 0.002;
    [mapView setRegion:mapScale animated:NO];
    
    [self.view addSubview:mapView];
    
    
    // annotation の追加
    NSArray *annotations = @[
        [[MyAnnotation alloc] initWithLocationCoordinate:appleStoreGinzaCoord
                                                   title:@"Apple Store"
                                                subtitle:@"GINZA"]
    ];
    [mapView addAnnotations:annotations];
}

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation{
    
    static NSString *PinIdentifier = @"Pin";
    MKPinAnnotationView *pin = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:PinIdentifier];
    if (pin == nil){
        pin = [[MKPinAnnotationView alloc]
               initWithAnnotation:annotation
               reuseIdentifier:PinIdentifier];
        pin.animatesDrop = YES;  // アニメーションをする
        pin.pinColor = MKPinAnnotationColorPurple;  // ピンの色を紫にする
        pin.canShowCallout = YES;  // ピンをタップするとコールアウトを表示
    }
    return pin;
}

@end

Objective-C: MyAnnotation.h

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

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *annotationTitle;
    NSString *annotationSubtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSString *annotationTitle;
@property (nonatomic, strong) NSString *annotationSubtitle;
- (id)initWithLocationCoordinate:(CLLocationCoordinate2D)_coordinate
                           title:(NSString *)_annotationTitle
                        subtitle:(NSString *)_annotationannSubtitle;
- (NSString *)title;
- (NSString *)subtitle;

@end

Objective-C: MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation
@synthesize coordinate;
@synthesize annotationTitle;
@synthesize annotationSubtitle;

- (NSString *)title {
    return annotationTitle;
}

- (NSString *)subtitle {
    return annotationSubtitle;
}

- (id)initWithLocationCoordinate:(CLLocationCoordinate2D)_coordinate
                           title:(NSString *)_annotationTitle
                        subtitle:(NSString *)_annotationSubtitle {
    coordinate = _coordinate;
    self.annotationTitle = _annotationTitle;
    self.annotationSubtitle = _annotationSubtitle;
    return self;
}

@end