MKMapView不更新用户位置图像 [英] MKMapView not updating user position image

查看:133
本文介绍了MKMapView不更新用户位置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MKMapView显示我在启动时的位置,但随后图像永远不会跟随我。位置更新,屏幕跟着我,但原始的用户位置图像留在后面。

My MKMapView shows my position at startup but then the image never 'follows' me. The location gets updated and the screen does follow me, but the original "User Location" image stays behind.

以下是一些代码段:

-(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] autorelease];

        if(annotation == mapView.userLocation) customPinView.image = [self rotate:[UIImage imageNamed:@"myCar.png"] orientation:UIImageOrientationUp];
        else customPinView.image = [UIImage imageNamed:@"randomPin.png"];

        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;
        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

-(void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D loc = [location coordinate];
    if(isFollowing)
        [myMapView setCenterCoordinate:loc];//Works
}

和我的 viewDidLoad 我打电话: [myMapView setShowsUserLocation:YES]; 哪个确实有效。

and in my viewDidLoad I do call: [myMapView setShowsUserLocation:YES]; which does work.

所以基本上我忽略了更新我的位置或者最有可能在我为当前位置绘制新图像的位置。

So basically somewhere I neglect updating my position or its most possibly where I draw the new image for my current position.

任何人都可以看到我遗失或错误的地方,因为它不遵循我的位置更新?

Can anyone possibly see what I am missing or doing wrong there for it to not follow my location updates?

谢谢。

推荐答案

目前尚不清楚这是否是问题,但viewForAnnotation方法看起来不正确。

It's not clear if this is the issue but the viewForAnnotation method doesn't look right.

仅在创建注释视图时设置注释图像。如果重新使用视图,则更新注释属性,但不更新图像。重新使用的视图可能是针对需要不同图像的不同类型的注释。

The annotation image is only being set when an annotation view is created. If a view is re-used, the annotation property is updated but not the image. It's possible that the re-used view is for an annotation of a different type requiring a different image.

该方法应如下所示:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString* AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView)
    {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];               
        pinView.animatesDrop = NO;
        pinView.canShowCallout = YES;
    }
    else
    {
        pinView.annotation = annotation;
    }

    if (annotation == mapView.userLocation) 
        pinView.image = [self rotate:[UIImage imageNamed:@"myCar.png"] orientation:UIImageOrientationUp];
    else 
        pinView.image = [UIImage imageNamed:@"randomPin.png"];

    return pinView;
}

这篇关于MKMapView不更新用户位置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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