Ios 自定义用户位置图像未在pin上居中

Ios 自定义用户位置图像未在pin上居中,ios,mkmapview,mapkit,mkannotationview,userlocation,Ios,Mkmapview,Mapkit,Mkannotationview,Userlocation,所以我真的很困惑。我用过: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString* AnnotationIdentifier = @"Annotation"; MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView de

所以我真的很困惑。我用过:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString* AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView) {
        MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        if (annotation == mapView.userLocation){
            customPinView.image = [UIImage imageNamed:@"303761_4417778996801_94858213_n.jpg"];
            [customPinView.layer setMasksToBounds:NO];
            [self setRoundedView:customPinView toDiameter:kPictureDiameter];
            [customPinView.layer setBorderColor:[UIColor whiteColor].CGColor];
            [customPinView.layer setBorderWidth:5.0f];
            [customPinView.layer setShadowColor:[UIColor blackColor].CGColor];
            [customPinView.layer setShadowOpacity:1.0f];
            [customPinView.layer setShadowRadius:20.0f];
            [customPinView.layer setShadowOffset:CGSizeMake(0, 0)];
            [customPinView setBackgroundColor:[UIColor whiteColor]];
        }
        return customPinView;
    } else {
        pinView.annotation = annotation;
    }

    return pinView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*AnnotationIdentifier=@“Annotation”;
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[mapView出列可重用AnnotationViewWithIdentifier:AnnotationIdentifier];
如果(!pinView){
MKPinAnnotationView*customPinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:AnnotationIdentifier];
if(注释==mapView.userLocation){
customPinView.image=[UIImage ImageName:@“303761_4417778996801_94858213_n.jpg”];
[customPinView.layer setMasksToBounds:否];
[自设置圆形视图:自定义PinView至直径:kPictureDiameter];
[customPinView.layer setBorderColor:[UIColor whiteColor].CGColor];
[customPinView.layer setBorderWidth:5.0f];
[customPinView.layer setShadowColor:[UIColor blackColor].CGColor];
[customPinView.layer setShadowOpacity:1.0f];
[customPinView.layer setShadowRadius:20.0f];
[customPinView.layer setShadowOffset:CGSizeMake(0,0)];
[customPinView setBackgroundColor:[UIColor whiteColor]];
}
返回customPinView;
}否则{
pinView.annotation=注释;
}
返回pinView;
}
将我自己的图像添加到用户位置pin。这就行了。但有两个问题:

  • 阴影不起作用

  • 我为用户位置pin设置的图像偏离中心,如下所示:

  • 我试过摆弄CGRect、CGPoint、设置帧、中心ect(尽管我还没有尝试改变边界),但什么都没有改变


    任何帮助都将不胜感激。

    关于图像偏离中心,有两种想法:

  • 在使用自己的图像进行注释时,我通常使用
    MKAnnotationView
    ,而不是
    MKPinAnnotationView
    。当我尝试使用
    MKPinAnnotationView
    时,我的图像不再居中

  • 如果出现最坏情况,您可以使用
    centerOffset
    属性。如果您使用
    MKAnnotationView
    ,我认为您不需要这样做

  • 关于你的影子:

  • 阴影半径为20是如此之大,以至于它是如此的漫反射以至于你几乎看不到它。我用了20块,几乎认不出来。它在10岁时开始变得更为明显,在5岁时更为明显。我不认为这是真正的问题,但当你开始实验时,在这里使用一个足够低的数字,这样你就能清楚地看到阴影

  • 话虽如此,我认为这不是问题所在。我怀疑图像舍入算法。你能试着暂时评论一下,看看你的影子是否出现了吗?但是我在
    MKAnnotationView
    图像上使用阴影,效果很好。你能和我们分享你的取整算法吗

  • 如果这不起作用,那么这张图像是否与覆盖层重合?(即图片后面的圆圈是什么)。我会再次尝试暂时移除它,看看这是否会改变行为。当我使用
    MKCircleView
    时,它工作得很好,但我们要确保我们没有在那里做任何奇怪的事情(或使用任何其他绘图例程)


  • 从Pin切换到常规注释是完美的。好主意。对于阴影,这是取整方法,因为它是在层上操作的。