原始地址: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