iOS - 本地图 View 发生变化(滚动、缩放)时,Mapkit 会保持用户位置动画

标签 ios animation mapkit repeat userlocation

我使用 CLLocationManager 的坐标在 map 上显示用户的位置。每次我移动或缩放 map 时,用户位置图钉(具有声纳效果的蓝色图钉)都会不断消失,然后重新动画回到 map 上。 (我不是指声纳动画效果。蓝点实际上消失了,然后重新动画)。

本地图滚动/缩放时,我没有调用任何其他方法。 使用断点,在map的委托(delegate)方法中只调用return nil:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    //User's Pin
    if([annotation class] == MKUserLocation.class) {
        return nil;
    }

....rest of my code here, nothing else is called as I am only plotting user at this time...
}

最佳答案

在我原来的答案中,我认为目标是替换使用 MKMapViewshowUserLocation = YES 时出现的闪烁蓝点userTrackingMode = MKUserTrackingModeFollow。因此,我展示了如何用图像或标准引脚替换它。

但事实证明,问题不在于有一个蓝点显示当前位置,而是它的动画被中断,并且随着用户在 map 上平移和缩放而出现和消失。

如果您调用 removeAnnotations 并删除所有注释(包括系统生成的 MKUserLocation 注释),我就会看到这种行为。如果您关闭 showUserLocation 并重新打开它,我也看到过这种行为。

OP 指出,这些情况都不适用,但对于 future 的读者来说,这些是可能导致此行为的一些考虑因素。


原答案:

最简单的答案是确保您的 Controller 是 MKMapView委托(delegate),然后定义一个 viewForAnnotation 来检测 >MKUserLocation,并将注释 View 替换为您想要的任何内容。例如,如果您想要显示一个 @"user.png" 图像,它可能如下所示:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        NSString *annotationIdentifier = @"userlocation";
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        if (annotationView)
        {
            annotationView.annotation = annotation;
        }
        else
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
            annotationView.image = [UIImage imageNamed:@"user.png"];
        }

        return annotationView;
    }

    // again, if you had other annotation types, such as MKPointAnnotation,
    // handle them here

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        ...
    }

    return nil;
}

或者,如果您想显示标准引脚,您可以:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
        {
            NSString *annotationIdentifier = @"userlocation";
            MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
            if (annotationView)
            {
                annotationView.annotation = annotation;
            }
            else
            {
                annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
            }

            return annotationView;
        }
    }

    // again, if you had other annotation types, such as MKPointAnnotation,
    // handle them here

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        ...
    }

    return nil;
}

关于iOS - 本地图 View 发生变化(滚动、缩放)时,Mapkit 会保持用户位置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16311472/

相关文章:

ios - 禁用计时器 iOS 上的按钮

ios - Swift UITableView 每 N 个单元格不同

python - 如何在pygame中为绘图设置动画(运动)

javascript - 在 $.animate 中使用 step() 不使用选择器来停止所有其他动画?

ios - 使用 MapKit 的地形和卫星 View

iphone - 在 iPhone MKMapView 中显示用户位置蓝点

ios - 在新线程中加载 UIWebView 内容

iphone - 加入多个 ALAC 文件。

ios - Swift:让省略号不断地在标签中重新输入?硬标签 "animation"?

ios - 我如何区分Mapkit当前使用的坐标系,特别是在中国?