在利用MKMapView显示自己当前位置的地图一文中介绍了放大地图和显示自己的位置,下面介绍一下在地图自己位置上添加大头针,实现如下所示:
实现的具体过程很简单:
首先实现MKMapViewDelegate协议:
@interface iphone_MapViewController : UIViewController
<CLLocationManagerDelegate,MKMapViewDelegate>{
IBOutlet MKMapView *mapView;
}
然后添加代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate=self;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
locationManager.delegate=self;//设置代理
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
locationManager.distanceFilter=1000.0f;//设置距离筛选器
[locationManager startUpdatingLocation];//启动位置管理器MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
theRegion.center=[[locationManager location] coordinate];
[locationManager release];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
theRegion.span.longitudeDelta = 0.01f;
theRegion.span.latitudeDelta = 0.01f;
[mapView setRegion:theRegion animated:YES];
}- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
[mapView.userLocation setTitle:@"欧陆经典"];
[mapView.userLocation setSubtitle:@"vsp"];
return pinView;
}
运行即可。