//
// 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