iPhone地図に線

地図のタップした2点に線をひくiPhoneアプリのサンプルコードを描いてみます。



#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>

@property (nonatomic, weak) MKMapView *map;

@property (nonatomic, strong) NSMutableArray *points;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.points = [NSMutableArray array];

    [self createMap];

}

– (void)createMap

{

    MKMapView *mv = [[MKMapView alloc] initWithFrame:self.view.bounds];

    mv.delegate = self;

    [self.view addSubview:mv];

    self.map = mv;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    CLLocationCoordinate2D coordinate = [self.map convertPoint:p toCoordinateFromView:self.view];

    

    [self.points addObject:[NSValue valueWithMKCoordinate:coordinate]];

    

    if (self.points.count % 2 == 0) {

        [self addLineOverlay];

    }

    [super touchesBegan:touches withEvent:event];

}

– (void)addLineOverlay

{

    CLLocationCoordinate2D  points[4];

    points[0] = [self.points[self.points.count2] MKCoordinateValue];

    points[1] = [self.points[self.points.count2] MKCoordinateValue];

    points[2] = [[self.points lastObject] MKCoordinateValue];

    points[3] = [[self.points lastObject] MKCoordinateValue];

    MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];

    poly.title = [NSString stringWithFormat:@”%lu”, self.points.count];

    [self.map addOverlay:poly];

}

– (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 redColor] colorWithAlphaComponent:0.5];

    renderer.strokeColor = [UIColor redColor];

    renderer.lineWidth = 10;

    

    int idx = [polygon.title  intValue];

    CLLocationCoordinate2D p1 = [self.points[idx-2] MKCoordinateValue];

    CLLocationCoordinate2D p2 = [self.points[idx-1] MKCoordinateValue];

    CLLocation *l1 = [[CLLocation alloc] initWithLatitude:p1.latitude longitude:p1.longitude];

    CLLocation *l2 = [[CLLocation alloc] initWithLatitude:p2.latitude longitude:p2.longitude];

    CLLocationDistance dist = [l1 distanceFromLocation:l2];

    

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

    annotation.coordinate = p2;

    annotation.title = [NSString stringWithFormat:@”%.1f”, dist / 1000];

    [self.map addAnnotation:annotation];

    

    return renderer;

}

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

{

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

        return nil;

    

    static NSString *reuseId = @”reuseid”;

    MKAnnotationView *a = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if (!a)

    {

        a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];

        

        UILabel *l = [[UILabel alloc] init];

        l.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];

        l.textColor = [UIColor greenColor];

        l.text = annotation.title;

        [l sizeToFit];

        [a addSubview:l];

        a.canShowCallout = YES;

        a.frame = l.frame;

        l.tag = 1;

    }

    else

    {

        a.annotation = annotation;

    }    

    return a;

}

@end