在ios开发iphone的程序中,需要在地图上标记一个或者多个点,同时点击以后可以切换到其他界面
开发步骤说明:
项目框架中加入 Mapkit Framework
在 地图上每一个记号,都是一個MKAnnotationView,也就是UI。而每一個MKAnnotationView都需要有对应的资料 MKAnnotation,这是Protocal,也就是存储每個座坐标所需要用到的资料的地方。因此,我们要先建立一個使用MKAnnotation的类别。
依照iPhone开发者文件的说明。这个Protocal需要声明三个属性和一个初始化方法。三个属性分別是coordinate、title、subtitle,和一个方法initWithCoords。
开发步骤:
- 定义一个MKAnnotation.
// // SD4SAnnotation.h // SelfDriving // // Created by 泽宇 徐 on 12-6-5. // Copyright (c) 2012年 WenBinGao. All rights reserved. // #import <Foundation/Foundation.h> #import "MapKit/MapKit.h" @interface SD4SAnnotation : NSObject<MKAnnotation> { CLLocationCoordinate2D coordinate; NSString * title; NSString * subtitle; //自己定义的其他信息成员 } @property(nonatomic,readonly) CLLocationCoordinate2D coordinate; @property(nonatomic,retain) NSString *title; @property(nonatomic,retain) NSString *subtitle; -(id) initWithCoordinate:(CLLocationCoordinate2D) temp_coordinate; @end
// // SD4SAnnotation.m // SelfDriving // // Created by 泽宇 徐 on 12-6-5. // Copyright (c) 2012年 WenBinGao. All rights reserved. // #import "SD4SAnnotation.h" @implementation SD4SAnnotation @synthesize coordinate; @synthesize title; @synthesize subtitle; -(id) initWithCoordinate:(CLLocationCoordinate2D) temp_coordinate { if ([super init]) { coordinate=temp_coordinate; } return self; } -(void) dealloc { [title release]; [subtitle release]; [super dealloc]; } @end
- 在viewController文件中实现显示地图,标记点,点击标记点后跳转的功能
viewController.h文件
// // SD4SListViewController.h // SelfDriving // // Created by WenBin Gao on 12-5-24. // Copyright (c) 2012年 WenBinGao. All rights reserved. // #import <UIKit/UIKit.h> #import "MapKit/MapKit.h" #import "SD4SAnnotation.h" @interface SD4SListViewController : SDBaseViewController<CLLocationManagerDelegate,MKMapViewDelegate> { IBOutlet MKMapView * mapView; NSMutableArray *mapAnnotations; } @property(nonatomic,retain) MKMapView * mapView; //地图控件 @property (nonatomic, retain) NSMutableArray *mapAnnotations; //标记点的数组 @end
在viewController.m文件中实现
// // SD4SListViewController.m // SelfDriving // // Created by WenBin Gao on 12-5-24. // Copyright (c) 2012年 WenBinGao. All rights reserved. // #import "SD4SListViewController.h" #import "SD4SDetailViewController.h" @interface SD4SListViewController () @end @implementation SD4SListViewController @synthesize mapView; @synthesize mapAnnotations; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.mapView.mapType = MKMapTypeStandard; // also MKMapTypeSatellite or MKMapTypeHybrid // create a custom navigation bar button and set it to always says "Back" // create out annotations array (in this example only 1) self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:1]; // annotation for the City of shanghai CLLocationCoordinate2D cLLocationCoordinate2D ; cLLocationCoordinate2D.latitude = 31.223078; cLLocationCoordinate2D.longitude = 121.4748; SD4SAnnotation * annotation = [[SD4SAnnotation alloc] initWithCoordinate:cLLocationCoordinate2D]; annotation.title=@"宝马4S"; annotation.subtitle=@"宝驴他哥"; [self.mapAnnotations insertObject:annotation atIndex:0]; [annotation release]; [self gotoLocation]; // finally goto shanghai [self.mapView removeAnnotations:self.mapView.annotations]; // remove any annotations that exist [self.mapView addAnnotations:self.mapAnnotations]; } - (void)gotoLocation { NSLog(@"定位上海"); // start off by default in San Francisco MKCoordinateRegion newRegion; newRegion.center.latitude = 31.223078; newRegion.center.longitude = 121.47480; newRegion.span.latitudeDelta = 0.112872; newRegion.span.longitudeDelta = 0.109863; [self.mapView setRegion:newRegion animated:YES]; } - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation { // if it's the user location, just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // handle our two custom annotations // if ([annotation isKindOfClass:[SD4SAnnotation class]]) { // try to dequeue an existing pin view first static NSString* shop4SAnnotationIdentifier = @"Shop4SAnnotationIdentifier";//4s店 MKPinAnnotationView* pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:shop4SAnnotationIdentifier]; if (!pinView) { // if an existing pin view was not available, create one MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:shop4SAnnotationIdentifier] autorelease]; customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; // add a detail disclosure button to the callout which will open a new view controller page // // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement: // - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; // UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; customPinView.rightCalloutAccessoryView = rightButton; return customPinView; } else { pinView.annotation = annotation; } return pinView; } return nil; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(void) dealloc { [self.mapAnnotations release]; [self.mapView release]; [super dealloc]; } @end
Annotation 集合中有几个坐标点viewForAnnotation方法就会被执行几次。因此每次viewForAnnotation被执行,我们都要实例化一个MKAnnotationView。