在不更改用户位置图像的情况下将图像添加到MKPointAnnotation

时间:2017-09-26 06:43:57

标签: ios objective-c mkmapview mkmapviewdelegate mkpointannotation

我正在使用this代码将图片添加到MKPointAnnotation。

    - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
    MKPinAnnotationView *pinView =
    (MKPinAnnotationView *)[_routeMap dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
    if (!pinView)
    {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:SFAnnotationIdentifier];
        UIImage *flagImage = [UIImage imageNamed:@"BikeIconMap"];
        // You may need to resize the image here.
        annotationView.image = flagImage;
        return annotationView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

但它也显示了用户位置的自定义图像。用户位置的圆形波动也消失了。

enter image description here enter image description here

有没有办法在不改变用户位置图像和动画的情况下将图像添加到MKPointAnnotation?

1 个答案:

答案 0 :(得分:1)

在MKMapViewDelegate的viewForAnnotation:方法中添加此代码

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation) return nil;