【问题标题】:UserLocation Image doesn't moveUserLocation 图像不移动
【发布时间】:2012-10-22 02:47:34
【问题描述】:

我不明白为什么当我在 userLocation 上设置服装图像时它没有移动。

如果我不设置图像,它会使用默认的蓝色图钉并且它可以工作;当位置改变时,我可以看到用户位置移动。

这是我从其他帖子中获取的 viewForAnnotation。

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

    if (annotation == mk.userLocation)
        pinView.image = [UIImage imageNamed:@"PositionPin.png"];
    else
        pinView.image = [UIImage imageNamed:@"PositionPin.png"];

    return pinView;
}    

【问题讨论】:

  • “mk”的定义是什么?
  • 如何将 pinView.animatesDrop 设置为 YES?
  • 不,pinView.animatesDrop 是 NO

标签: ios mkmapview mapkit


【解决方案1】:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString* AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mk dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView)
    {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
        pinView.animatesDrop = NO;
        pinView.canShowCallout = YES;
    }
        pinView.annotation = annotation;
        pinView.image = [UIImage imageNamed:@"PositionPin.png"];

    return pinView;
}    

【讨论】:

  • 感谢您的回复,但它只有在我放大时才会移动,您知道为什么它不会自动移动吗?
【解决方案2】:

这似乎是地图视图中的一个错误(仍然存在于 iOS 6 中),当使用自定义注释视图时用户位置坐标不再更新(即使文档表明它会起作用)。

一种解决方法是使用核心位置获取用户位置更新并创建您自己的注释(而不是使用地图视图的userLocation),然后在viewForAnnotation 中为其提供自定义图像。

请参阅Custom Annotation view for userlocation not moving the mapview 的答案以获取更多详细信息和一些示例代码来实现解决方法。


此外,请注意,在注释视图中使用自定义图像时,应使用MKAnnotationView 而不是MKPinAnnotationView
使用MKPinAnnotationView 时,有时会显示其默认图钉图像而不是您的自定义图像。

【讨论】:

    猜你喜欢
    • 2016-11-23
    • 2012-07-14
    • 2022-01-25
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多