iPhone山手線塗り

地図で山手線エリアを囲むようなiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>

@property (nonatomic, weak) MKMapView *map;

@property (nonatomic, strong) NSMutableArray *placemarks;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createMap];

    [self createButton];

}

– (void)createMap

{

    MKMapView *mv = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, CGRectGetMaxY(self.view.bounds) – 80)];

    mv.delegate = self;

    [self.view addSubview:mv];

    self.map = mv;

}

– (void)createButton

{

    NSArray *names = @[@”showPin”, @”ShowArea”];

    SEL selectors[] = {@selector(showPin), @selector(showArea)};

    

    for (int i=0; i<names.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        [btn setTitle:names[i] forState:UIControlStateNormal];

        [btn sizeToFit];

        btn.center = CGPointMake(80 + i*160, CGRectGetMaxY(self.view.frame) – 40);

        [self.view addSubview:btn];

        [btn addTarget:self action:selectors[i] forControlEvents:UIControlEventTouchUpInside];

    }

}

– (void)showArea

{

    CLLocationCoordinate2D  points[self.placemarks.count];

    for (int i=0; i<self.placemarks.count; i++) {

        CLLocationCoordinate2D cl = [self.placemarks[i] coordinate];

        points[i] = cl;

    }

    MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:self.placemarks.count];

    poly.title = @”yamanote area”;

    [self.map addOverlay:poly];

}

– (void)showPin

{

    NSArray *stationNames = [@”大崎 五反田 目黒 恵比寿 渋谷 原宿 代々木 新宿 新大久保 高田馬場 目白 池袋 大塚 巣鴨 駒込 田端 西日暮里 日暮里 鶯谷 上野 御徒町 秋葉原 神田 東京 有楽町 新橋 浜松町 田町 品川 componentsSeparatedByString:@” “];

    self.placemarks = [NSMutableArray array];

    

    for (int i=0; i<stationNames.count; i++) {

        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            MKLocalSearchRequest *req = [[MKLocalSearchRequest alloc] init];

            req.naturalLanguageQuery = [NSString stringWithFormat:@”%@, stationNames[i]];

            MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:req];

            

            [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

                for (MKMapItem *item in response.mapItems) {

                    [self.placemarks addObject:item.placemark];

                }

                [self.map showAnnotations:self.placemarks animated:YES];

            }];

        });

    }

}

– (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

{

    if (![overlay isKindOfClass:[MKPolygon class]]) {

        return nil;

    }

    MKPolygon *polygon = (MKPolygon *)overlay;

    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];

    renderer.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];

    renderer.strokeColor = [UIColor greenColor];

    renderer.lineWidth = 5;

    return renderer;

}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[MKUserLocation class]])

    {

        return nil;

    }

    

    static NSString *reuseId = @”pin”;

    MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if (pav == nil)

    {

        pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];

        pav.canShowCallout = YES;

        pav.animatesDrop = YES;

        pav.pinColor = MKPinAnnotationColorGreen;

    }

    else

    {

        pav.annotation = annotation;

    }

    return pav;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end