首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKUserLocation自定义视图未移动!

MKUserLocation自定义视图未移动!
EN

Stack Overflow用户
提问于 2011-04-24 15:51:29
回答 4查看 3.4K关注 0票数 7

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

代码语言:javascript
复制
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

{

代码语言:javascript
复制
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

代码语言:javascript
复制
       - (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];
}

问题是它只是停留在同一位置,而不是像蓝点那样移动。我做错了什么?

谢谢比尔。

EN

回答 4

Stack Overflow用户

发布于 2011-07-15 12:21:25

我刚才也遇到了这个问题。我不确定这是否是预期的行为,但无论出于什么原因,都要由我们来决定是否移动自定义的MKUserLocation注释视图。

一个简单的解决方案是

代码语言:javascript
复制
- (void) locationController: (LocationController *) locationController
        didUpdateToLocation: (CLLocation *) location
{
    [[self mapView] setShowsUserLocation:NO];

    [[self mapView] setShowsUserLocation:YES];
}

但这使得当前位置注释在屏幕上跳转,这是我不希望看到的。

更好的做法是在视图控制器中保留对自定义注释视图的引用作为ivar,然后执行以下操作:

代码语言:javascript
复制
- (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];
    }];

}

这很好,除非当您更改缩放级别时,注释停留在相对于地图视图的矩形的位置,然后仅在缩放或平移完成后才会动画到正确的位置。最好遵循Apple的做法,在区域更改期间,使当前位置注释消失并重新出现:

代码语言:javascript
复制
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    [[self mapView] setShowsUserLocation:NO];
}

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

Stack Overflow用户

发布于 2011-04-28 05:00:18

比尔,

您需要一个已使用适用的desiredAccuracy和distanceFilter初始化的CLLocationManager,然后实现适用的委托方法,并将您自己的代码设置为locationManager实例上的委托。

您至少应该具有以下方法,一旦Location Manager确定了desiredAccuracy中的当前位置,它将调用该方法,然后在满足distanceFilter时再次调用该方法。

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation ;

干杯,麦克

票数 0
EN

Stack Overflow用户

发布于 2012-08-18 10:51:06

我刚刚找到了这个问题的答案。我的代码是用Monotouch编写的,但是用ObjC重新编写应该很容易。要在默认MKUserLocation上显示自定义图像,我们需要在原始视图上添加一个子视图。为此,请在MKMapView委托中重写DidAddAnnotationViews

代码语言:javascript
复制
void DidAddAnnotationViews (object sender, MKMapViewAnnotationEventArgs e)
{
            MKAnnotationView v = mapView.ViewForAnnotation(mapView.UserLocation);
            if(v != null)
            {
                if(v.Subviews.Count() == 0)
                {
                    UIImageView iv = new UIImageView(new RectangleF(0,0, 22, 22));
                    iv.Image = UIImage.FromFile("res/pins/Yhere.png");
                    v.AddSubview(iv);
                    v.BringSubviewToFront(iv);
                }
            }
}

这使得自定义图像在蓝点上移动。更重要的是,用户跟踪和位置更新功能工作得很好,你仍然可以看到蓝色的圆圈表示位置的准确性。

祝你享受定制MKUserLocation的乐趣!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5769363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档