2

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

滚动/缩放地图时,我不会调用任何其他方法。使用断点,在 map 的委托方法中只调用 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...
}
4

3 回答 3

3

在我最初的答案中,我认为目标是替换使用 a 时得到的闪烁蓝MKMapViewshowUserLocation = YESuserTrackingMode = MKUserTrackingModeFollow。因此,我展示了如何用图像或标准图钉替换它。

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

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

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


原答案:

最简单的答案是确保您的控制器delegate适用于您的MKMapView,然后定义viewForAnnotation检测 a 的 a MKUserLocation,并将注释视图替换为您想要的任何内容。例如,如果您有一张@"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;
}
于 2013-05-01T03:08:48.337 回答
0

如果您使用标准mapciew.showsUserLocation = true;绘制用户的位置并且它在屏幕上滑动而不是在原地跳动,那么这可能是因为您的viewForAnnotation方法。确保您只为您添加的图钉设置动画,而不是地图视图添加的用户位置。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
      return nil;
    }
    //do stuff to the annotations that you added
}
于 2013-05-01T04:18:44.660 回答
0

在 viewForAnnotation 委托方法下捕获默认的用户位置注释,因此它不会被绘制。您可以使用布尔值来打开和关闭它,或者与 LocationManager 一起创建您自己的自定义注释。这个链接应该让你走上正轨:

用户位置 Anno

于 2013-05-01T01:41:33.713 回答