地図上の好きな場所をコレクションしていくようなiPhoneアプリのサンプルコードを描いてみます。
動かすとこうなります。
サンプルコード
#import “ViewController.h”
#import <MapKit/MapKit.h>
@interface ViewController ()
@property (nonatomic, weak) MKMapView *mapView;
@property (nonatomic, strong) NSMutableArray *mapCollection;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor darkGrayColor];
[self createMainMap];
[self createCollectionBox];
[self tokyoStation];
}
– (void)createMainMap
{
MKMapView *view = [[MKMapView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];
[self.view addSubview:view];
self.mapView = view;
}
– (void)tokyoStation
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@”Tokyo Station” completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count) {
CLPlacemark *placemark = placemarks[0];
float spanX = 0.005;
float spanY = 0.005;
MKCoordinateRegion region;
region.center.latitude = placemark.location.coordinate.latitude;
region.center.longitude = placemark.location.coordinate.longitude;
region.span = MKCoordinateSpanMake(spanX, spanY);
[self.mapView setRegion:region animated:YES];
}
}];
}
– (void)createCollectionBox
{
for (int i=0; i<6; i++) {
float x = (i % 3) * 100 + 15;
float y = (i / 3) * 100 + 350;
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(x, y, 90, 90)];
box.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
[self.view addSubview:box];
UILabel *l = [[UILabel alloc] init];
l.text = @”+”;
l.font = [UIFont boldSystemFontOfSize:60];
[l sizeToFit];
l.center = CGPointMake(CGRectGetMidX(box.bounds), CGRectGetMidY(box.bounds) – 5);
l.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
[box addSubview:l];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectMap:)];
[box addGestureRecognizer:tap];
}
}
– (void)collectMap:(UITapGestureRecognizer*)gr
{
CLLocationCoordinate2D touchInMap = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.view];
if (!self.mapCollection) {
self.mapCollection = [NSMutableArray array];
}
[self.mapCollection addObject:[NSValue valueWithMKCoordinate:touchInMap]];
// screenshot
UIGraphicsBeginImageContext(self.mapView.frame.size);
[[self.mapView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *thumbnail = [[UIImageView alloc] initWithImage:mapImage];
thumbnail.frame = gr.view.bounds;
[gr.view addSubview:thumbnail];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCollection:)];
[gr.view addGestureRecognizer:tap];
gr.view.tag = self.mapCollection.count;
}
– (void)showCollection:(UITapGestureRecognizer*)gr
{
float spanX = 0.005;
float spanY = 0.005;
MKCoordinateRegion region;
region.center = [self.mapCollection[gr.view.tag – 1] MKCoordinateValue];
region.span = MKCoordinateSpanMake(spanX, spanY);
[self.mapView setRegion:region animated:YES];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end