MKUserLocation自定义视图不动! [英] MKUserLocation Custom View not moving!

查看:98
本文介绍了MKUserLocation自定义视图不动!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为用户位置创建了一个自定义MKAnnotationView:

I have created a custom MKAnnotationView for User Location:

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

{

if ([annotation isKindOfClass:[MKUserLocation class]]) {

    if (navStatus == NavStatusHeadingEnabled) {

        if ([annotation isKindOfClass:[MKUserLocation class]]) {

            locationView = [[CustomLocationView alloc] initWithAnnotation:annotation reuseIdentifier:@"locationIdentifier"];
            return locationView;

        }

    }

    return nil;

}

CustomLocationView.h

CustomLocationView.h

       - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        if (self != nil)
        {


            self.backgroundColor = [UIColor clearColor];
            blueDot = [UIImage imageNamed:@"userLocationDot.png"].CGImage;
            CGImageRetain(blueDot);

            CGPoint blueDotCenter = CGPointMake((self.frame.size.width - (CGImageGetWidth(blueDot) / 2)) / 2, (self.frame.size.height - (CGImageGetHeight(blueDot) / 2)) / 2);

            blueDotLayer = [CALayer layer];
            blueDotLayer.frame = CGRectMake(blueDotCenter.x, blueDotCenter.y , CGImageGetWidth(blueDot) / 2, CGImageGetHeight(blueDot) / 2);
            blueDotLayer.contents = (id)blueDot;
            blueDotLayer.shadowOpacity = 0.4;
            blueDotLayer.shadowColor = [UIColor blackColor].CGColor;
            blueDotLayer.shadowOffset = CGSizeMake(0.4, 0.3);
            blueDotLayer.shadowRadius = 1.0f;

            [self.layer insertSublayer:blueDotLayer above:self.layer];

        }

        return self;
    }

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];

    [self setNeedsDisplay];
}





- (void)dealloc {

    [blueDotLayer release];


    [super dealloc];
}

问题是它只是停留在同一个地方而不是像蓝色一样移动点。
我做错了什么?

The problem is it just stays on the same place and not moving like the blue dot. What I am doing wrong?

谢谢
比尔。

推荐答案

我刚刚遇到了这个问题。我不确定这是否是预期的行为,但无论出于何种原因,我们都应该移动我们的自定义MKUserLocation注释视图。

I ran into this problem just now as well. I'm not sure if this is expected behavior or not, but for whatever reason it is up to us to move our custom MKUserLocation annotation views.

一个天真的解决方案是

- (void) locationController: (LocationController *) locationController
        didUpdateToLocation: (CLLocation *) location
{
    [[self mapView] setShowsUserLocation:NO];

    [[self mapView] setShowsUserLocation:YES];
}

但是这会使当前位置注释在屏幕上跳转,我发现这是不合需要的。

But this makes the current location annotation jump around the screen which I found undesirable.

最好是在视图控制器中将自定义注释视图的引用保存为ivar然后执行:

Better yet is to keep a reference to the custom annotation view as an ivar in your view controller and then do:

- (void) locationController: (LocationController *) locationController
        didUpdateToLocation: (CLLocation *) location
{
    CGPoint newCenterPoint = [[self mapView] convertCoordinate:[location coordinate] toPointToView:[[self customAnnotationView] superview]];
    newCenterPoint.x += [[self customAnnotationView] centerOffset].x;
    newCenterPoint.y += [[self customAnnotationView] centerOffset].y;

    [UIView animateWithDuration:0.3f animations:^{
        [[self customAnnotationView] setCenter:newCenterPoint];
    }];

}

这很好,除非您更改缩放级别的注释保持相对于地图视图的rect的位置,然后仅在缩放或平移完成后动画到正确的位置。最好跟随Apple的领先优势并使当前位置注释消失并在区域更改期间重新出现:

This is good except when you change the zoom level the annotation stays where it was relative to the map view's rect and then animates to the correct location only after the zoom or pan is complete. Best to follow Apple's lead and make the current location annotation disappear and reappear during region changes:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    [[self mapView] setShowsUserLocation:NO];
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    [[self mapView] setShowsUserLocation:YES];
}

这篇关于MKUserLocation自定义视图不动!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆