这是一个基本的例子,就像几个步骤一样简单
1)在其中添加以下注释 viewDidLoad 会工作得很好
viewDidLoad
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init]; CLLocationCoordinate2D c1; c1.latitude = 46.469391; c1.longitude = 30.740883; point1.coordinate = c1; point1.title = @"Minsk, Belarus"; [self.mapView addAnnotation:point1]; MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init]; CLLocationCoordinate2D c2; c2.latitude = 46.469391; c2.longitude = 30.740883; point2.coordinate = c2; point2.title = @"Odessa, Ukraine"; [self.mapView addAnnotation:point2];
2)在mapView中:MKMapViewDelegate的viewForAnnotation为注释提供可重用的视图,如下所示:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKPointAnnotation class]]) { MKMarkerAnnotationView* annotationView = (MKMarkerAnnotationView *) (MKMarkerAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"Jacky.S"]; if (annotationView == nil) { annotationView = [[MKMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Jacky.S"]; annotationView.enabled = YES; annotationView.clusteringIdentifier = @"pins"; // annotationView.glyphImage = [UIImage imageNamed:@"we can use a nice image instead of the default pins"]; } else { annotationView.annotation = annotation; annotationView.clusteringIdentifier = @"pins"; } return annotationView; } return nil; }
别忘了设置 MKMapViewDelegate 至 UIViewController
MKMapViewDelegate
UIViewController
[self.mapView setDelegate:self];
的 更新 强> 刚刚发完了一个 要旨 展示了如何子类化 MKMarkerAnnotationView
MKMarkerAnnotationView