原始地址:iOS 6苹果地图应用(MapKit)-内置开发
本文是苹果案例RegionDefiner的注释。
#import "ViewController.h" #import "MyAnnotation.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.mainMapView = [[MKMapView alloc] init]; self.mainMapView.delegate = self; [self.mainMapView setUserInteractionEnabled: YES]; [self.mainMapView setScrollEnabled: YES]; UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth; [toolbar sizeToFit]; CGSize containerSize = self.view.bounds.size; CGSize toolbarSize = toolbar.bounds.size; UIBarButtonItem *resetButton = [[UIBarButtonItem alloc] initWithTitle:@"Reset" style: UIBarButtonItemStyleBordered target: self action: @selector(removePins)]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithTitle: @"Log" style: UIBarButtonItemStyleBordered target: self action: @selector(tappedShare)]; toolbar.frame = CGRectMake(0, containerSize.height - toolbarSize.height, containerSize.width, toolbarSize.height); self.mainMapView.frame = CGRectMake(0, 0, containerSize.width, containerSize.height - toolbarSize.height); [self.view addSubview:self.mainMapView]; [self.view addSubview:toolbar]; //工具条上添加"重置按钮","空白","日志按钮" [toolbar setItems:@[ resetButton, flexibleSpace, share ]]; [self setUpGesture]; self.itemsArray = [[NSMutableArray alloc] init]; } - (void)viewDidUnload { [self setMainMapView:nil]; [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; } } #pragma mark - #pragma mark Dropping pins //添加长按手势 - (void)setUpGesture { self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)]; self.longPress.delegate = self; [self.view addGestureRecognizer:self.longPress]; } //处理长按手势 - (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { CGPoint longPressPoint = [recognizer locationInView:self.view]; //在此点绘制大头针 [self dropPinAtPoint:longPressPoint]; } } - (void)updatePolygon { CLLocationCoordinate2D *points = malloc(sizeof(CLLocationCoordinate2D) * self.itemsArray.count); NSUInteger i = 0; for (MyAnnotation *pin in self.itemsArray) { //添加所有坐标的经纬度 points[i] = pin.coordinate; i++; } //清除地图上上次绘制的图形 [self.mainMapView removeOverlay:self.polygon]; //绘制多边形 self.polygon = [MKPolygon polygonWithCoordinates:points count:self.itemsArray.count]; [self.mainMapView addOverlay:self.polygon]; } - (void) dropPinAtPoint: (CGPoint) pointToConvert { //把视图上的点转换成经纬度 CLLocationCoordinate2D convertedPoint = [self.mainMapView convertPoint: pointToConvert toCoordinateFromView: self.view]; //大头针的标题 NSString *pinTitle = [NSString stringWithFormat: @"Pin number %i", self.itemsArray.count]; //大头针的子标题,显示经纬度 NSString *subCoordinates = [NSString stringWithFormat: @"%f, %f", convertedPoint.latitude, convertedPoint.longitude]; MyAnnotation *droppedPin = [[MyAnnotation alloc] initWithCoordinate: convertedPoint title: pinTitle subtitle: subCoordinates]; //在地图上添加这个标记 [self.mainMapView addAnnotation:droppedPin]; //加入数组 [self.itemsArray addObject:droppedPin]; //更新多边形 [self updatePolygon]; } - (void)removePins { //移除标记 [self.mainMapView removeAnnotations:self.mainMapView.annotations]; //重置 [self.itemsArray removeAllObjects]; //清除绘制的多边形 [self.mainMapView removeOverlay:self.polygon]; //更新多边形 [self updatePolygon]; } #pragma mark - Output //操作日志 - (void)tappedShare { NSLog(@"%@", [self coordinates]); } - (NSString *)coordinates { //绘制多边形至少需要三个点 if (self.itemsArray.count < 3) { return @"Minimum of 3 vertices requried to make polygon."; } NSString *masterString = @"\n{ \"type\": \"MultiPolygon\",\n \"coordinates\": [\n[["; for (MyAnnotation *pin in self.itemsArray) { masterString = [masterString stringByAppendingFormat: @"[%f, %f],\n", pin.coordinate.longitude, pin.coordinate.latitude]; } // GeoJSON requires that the first and last vertices be identical MyAnnotation *firstPin = [self.itemsArray objectAtIndex:0]; masterString = [masterString stringByAppendingFormat: @"[%f, %f],\n", firstPin.coordinate.longitude, firstPin.coordinate.latitude]; masterString = [masterString stringByAppendingString: @"]]\n]\n}\n"]; masterString = [masterString stringByReplacingOccurrencesOfString: @"],\n]]" withString: @"]]]"]; return masterString; } #pragma mark - MKMapViewDelegate - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay { if (self.polygonView && self.polygonView.polygon == self.polygon) return self.polygonView; self.polygonView = [[MKPolygonView alloc] initWithPolygon:self.polygon]; //填充色 self.polygonView.fillColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.3f]; //边界颜色 self.polygonView.strokeColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.9f]; //边线宽度 self.polygonView.lineWidth = 1.0f; return self.polygonView; } @end