如何更改我的 MKMapView 注释的外观

How to change the look of my MKMapViewAnnotation

我已经设置了我的地图区域并在其中添加了图钉,但是我想知道是否可以更改图钉的外观?

我想让它看起来像将 ShowUsersLocation 设置为 YES 时出现的图标。

这是我的代码?

MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:mycoordinate];
region.center.latitude = mycoordinate.latitude;
region.center.longitude = mycoordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[mapView addAnnotation:annotation];
[mapView setRegion:region animated:NO];

实施- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation委托方法。 See the Documentation.

  1. 将地图视图的委托设置为 class 您设置的图钉(例如您的控制器)。
  2. 实施 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 委托方法。

基本实现:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    // Leave the user location pin as it is
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    static NSString *annotationReuseIdentifier = @"Annotation";
    MKAnnotationView *annotationView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationReuseIdentifier];
    }
    annotationView.annotation = annotation;

    // Set up your annotation view here

    return annotationView;
}

如果您想更好地控制注释的外观,您可以实现自己的注释视图(作为 MKAnnotationView 的子class)。

要像用户位置点一样显示图钉,制作点的图像并将其添加到注释视图或使用 QuartzCore 框架绘制点。