我使用 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...
}
最佳答案
在我原来的答案中,我认为目标是替换使用 MKMapView
和 showUserLocation = 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/