地图图层添加

//

//  ViewController.m

//  MapOverLayTest

//

//  Created by apple on 15/7/21.

//  Copyright (c) 2015 BJCA. All rights reserved.

//


#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "MyAnotion.h"

#import "DrawView.h"


@interface ViewController ()<MKMapViewDelegate>

{

    CLLocationCoordinate2D   coordinate;

    MKCoordinateRegion      region;

    MKCoordinateSpan        span;

}


@property(nonatomic,strong)    MKMapView    *mapView;



@property(nonatomic,strong)NSMutableArray   *arrayPoint;

@property(nonatomic,strong)MKPolygon        *polaygon;


@property(nonatomic,strong)DrawView         *drawV;


@end



@implementation ViewController



-(DrawView *)drawV {

    if (!_drawV) {

        _drawV = [[DrawView alloc]initWithFrame:self.view.frame];

        _drawV.backgroundColor = [[UIColor orangeColor]colorWithAlphaComponent:0.4];

    }

    return _drawV;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.arrayPoint = [NSMutableArray array];

    

    [self.view addSubview:self.mapView];

    

    coordinate = CLLocationCoordinate2DMake(31.216515, 121.538985);

    span.latitudeDelta = 0.1;

    span.longitudeDelta = 0.1;

    

    region.center = coordinate;

    region.span   = span;

    

    MyAnotion *ano = [[MyAnotion alloc]init];

    ano.title = @"title";

    ano.subtitle = @"subTitle";

    ano.coordinate = coordinate;

    

    

    CLLocationCoordinate2D libComPark[5];

    libComPark[0] = CLLocationCoordinate2DMake(31.216515,121.538985);

    libComPark[1] = CLLocationCoordinate2DMake(31.187170,121.422438);

    libComPark[2] = CLLocationCoordinate2DMake(31.220142,121.445540);

    libComPark[3] = CLLocationCoordinate2DMake(31.193884,121.394430);

    libComPark[4] = CLLocationCoordinate2DMake(31.292824,121.294420);

    

    MKPolygon *polLibcomPark = [MKPolygon polygonWithCoordinates:libComPark count:5];

    [self.mapView addOverlay:polLibcomPark];


    [self.mapView setRegion:region];

    [self.mapView addAnnotation:ano];

    

    [self.view addSubview:self.drawV];

    // Do any additional setup after loading the view, typically from a nib.

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.arrayPoint removeAllObjects];

    [self.mapView removeOverlay:self.polaygon];

    

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

    [self.arrayPoint addObject:[NSValue valueWithCGPoint:point]];

}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

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

    [self.arrayPoint addObject:[NSValue valueWithCGPoint:point]];

}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CLLocationCoordinate2D coor[[self.arrayPoint count]];

    CLLocationCoordinate2D coorSingle;

    for (int i = 0; i < [self.arrayPoint count]; i++) {

        coorSingle = [self.mapView convertPoint:[self.arrayPoint[i] CGPointValue] toCoordinateFromView:self.view];

        coor[i] = coorSingle;

        coor[i] = CLLocationCoordinate2DMake(coorSingle.latitude, coorSingle.longitude);

    }

    

    self.polaygon = [MKPolygon polygonWithCoordinates:coor count:[self.arrayPoint count]];

    [self.mapView addOverlay:self.polaygon];

}



- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay

{

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

        MKPolygonView* aView = [[MKPolygonView alloc]initWithPolygon:(MKPolygon*)overlay];

        aView.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];

        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];

        aView.lineWidth = 3;

        return aView;

    }

    return nil;

}


-(MKMapView *)mapView {

    if (!_mapView) {

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

        _mapView.delegate = self;

        _mapView.scrollEnabled = NO;

    }

    return _mapView;

}

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

    MKAnnotationView *view = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"re"];

    view.image = [UIImage imageNamed:@"qw.gif"];

    

    UIImageView *imagV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];

    imagV.image = [UIImage imageNamed:@"qw.gif"];

    view.leftCalloutAccessoryView = imagV;

    

    view.canShowCallout = YES;

    return view;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值