トップ «前の日記(2012-04-21) 最新 次の日記(2012-04-23)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-04-22 [iOS]地図を扱う

地図を表示するのは簡単だ。MapKitフレームワークをプロジェクトに追加し、MapKit/MapKit.hをインポートする。そして、以下のコードで地図は表示される。

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    CGRect  rect = CGRectMake(10.0, 80.0, 300.0, 300.0);
    self.mapView = [[MKMapView alloc] initWithFrame:rect];
    [self.view addSubview:self.mapView];
}
 
- (void)viewDidUnload
{
    self.mapView = nil;
 
    [super viewDidUnload];
}

viewDidLoadメソッドに以下のコードを追加すると、指定した緯度経度が表示される。

    MKCoordinateRegion  region = {{34.406944, 133.195462}, {1.0, 1.0}};
    [self.mapView setRegion:region animated:NO];

以下のコードで、ピンが追加される。

@interface ViewController : UIViewController 
@property (strong, nonatomic) MKMapView *mapView;
@end
 
@interface Annotation : NSObject 
@property (strong, nonatomic) NSString                  *name;
@property (assign, nonatomic) CLLocationCoordinate2D    coordinate;
- (id)initWithName:(NSString *)name latitude:(double)latitude longitude:(double)longitude;
@end
 
@implementation Annotation
@synthesize name = _name;
@synthesize coordinate = _coordinate;
- (id)initWithName:(NSString *)name latitude:(double)latitude longitude:(double)longitude
{
    if ((self = [super init]) != nil) {
        CLLocationCoordinate2D    coordinate;
        coordinate.latitude = latitude;
        coordinate.longitude = longitude;
        self.coordinate = coordinate;
        self.name = name;
    }
    return self;
}
 
- (NSString *)title
{
    return self.name;
}
 
- (void)dealloc
{
    self.name = nil;
}
@end
 
@implementation ViewController
@synthesize mapView = _mapView;
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    CGRect  rect = CGRectMake(10.0, 80.0, 300.0, 300.0);
    self.mapView = [[MKMapView alloc] initWithFrame:rect];
    [self.view addSubview:self.mapView];
    
    MKCoordinateRegion  region = {{34.406944, 133.195462}, {1.0, 1.0}};
    [self.mapView setRegion:region animated:NO];
    
    Annotation  *annotation = [[Annotation alloc] initWithName:@"土堂小学校"
                                                     latitude:34.406944
                                                    longitude:133.195462];
    [self.mapView addAnnotation:annotation];
}
 
- (void)viewDidUnload
{
    self.mapView = nil;
 
    [super viewDidUnload];
}
 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    if (annotation == self.mapView.userLocation) {
        return nil;
    }
     
    MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
    if (pinAnnotationView == nil) {
        pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    }
    else {
        pinAnnotationView.annotation = annotation;
    }
    
    pinAnnotationView.pinColor = MKPinAnnotationColorRed;
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.canShowCallout = YES;
    return pinAnnotationView;
}

実行。

Maps

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/Maps - GitHub

トップ «前の日記(2012-04-21) 最新 次の日記(2012-04-23)» 編集