xcode 如何阻止 viewForAnnotation 方法覆盖 iOS 中的默认用户位置蓝色信标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11767240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop the viewForAnnotation method from overriding the default user location blue beacon in iOS
提问by Hudson Buddy
Right now I am populating a map view with annotations, and also displaying the user's current location. With the viewForAnnotation method, it overrides all annotations with the default red pin, but I want to return the views for the other annotations, but keep the user location the default blue beacon. Is there a way to do this simply or do I have to create a new annotation view and return the right one depending on the annotation?
现在我正在用注释填充地图视图,并显示用户的当前位置。使用 viewForAnnotation 方法,它使用默认的红色 pin 覆盖所有注释,但我想返回其他注释的视图,但将用户位置保留为默认的蓝色信标。有没有办法简单地做到这一点,或者我必须创建一个新的注释视图并根据注释返回正确的视图?
Right now I have something like:
现在我有类似的东西:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation.coordinate == locationManager.location.coordinate) {
return nil;
}else {
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Beacon"];
// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
annotationView.rightCalloutAccessoryView = button;
annotationView.canShowCallout = YES;
return annotationView;
}
}
But I can't equate the two because I can't get the coordinate property out of the annotation argument.
但是我不能将两者等同起来,因为我无法从注释参数中获取坐标属性。
Anybody know any solutions to this?
任何人都知道任何解决方案?
回答by calimarkus
Check out the documentation here:
在此处查看文档:
As it states:
正如它所说:
If the object in the annotation parameter is an instance of the
MKUserLocation
class, you can provide a custom view to denote the user's location. To display the user's location using the default system view, return nil.
如果 annotation 参数中的对象是
MKUserLocation
类的实例,则 可以提供自定义视图来表示用户的位置。要使用默认系统视图显示用户的位置,请返回 nil。
So you can add a conditional to check for this:
所以你可以添加一个条件来检查这个:
if([annotation isKindOfClass: [MKUserLocation class]]) {
return nil;
}
回答by HalR
Swift 5.0
斯威夫特 5.0
Put a check at the top of the function to ignore the userLocation:
在函数顶部检查以忽略 userLocation:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation as? MKUserLocation != mapView.userLocation else { return }