Iphone 如何在地图注释(pin)上自动显示标题/副标题
我正在将注释加载到地图视图中。加载地图时,注释显示为接点 但是,标题和副标题不会自动显示在pin上。目前,要求用户在显示标题之前点击pin 有没有办法让标题在地图加载时自动显示在pin上 (这个问题几乎是一样的,但不是完全一样:因为我已经在我的对象中定义了-title和-subtitle属性。) 谢谢要调用的方法是从MKMapView中选择“Annotation:animated”Iphone 如何在地图注释(pin)上自动显示标题/副标题,iphone,map,annotations,Iphone,Map,Annotations,我正在将注释加载到地图视图中。加载地图时,注释显示为接点 但是,标题和副标题不会自动显示在pin上。目前,要求用户在显示标题之前点击pin 有没有办法让标题在地图加载时自动显示在pin上 (这个问题几乎是一样的,但不是完全一样:因为我已经在我的对象中定义了-title和-subtitle属性。) 谢谢要调用的方法是从MKMapView中选择“Annotation:animated” - (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NS
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,350,350);
[mv setRegion:region animated:YES];
[mapView selectAnnotation:mp animated:YES];
}
之后
从iOS 11中开始,有一种新类型的
MKAnnotationView
称为mkmarkernotationview
,它可以显示标题和副标题,而无需选择。检查
添加一个后续答案(适用于Xcode 11.4和Swift 5),因为我遇到了这个问题,但上面的答案不起作用@zsani是正确的,因为您需要使用mkmarkernotationview
(与MKPinAnnotationView
相反)来获取这两个属性,但是您还必须设置标题可访问性和字幕可访问性属性(尽管标题可访问性在默认情况下与mkmarkernotationview
相关).
分享,以防其他人也有同样的问题
[mapView selectAnnotation:mp animated:YES];
[mv setRegion:region animated:YES];
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
if #available(iOS 11.0, *) {
let annoView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnno")
annoView.canShowCallout = true
return annoView
}
let annoView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnnoLow")
annoView.canShowCallout = true
return annoView
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// do not alter user location marker
guard !annotation.isKind(of: MKUserLocation.self) else { return nil }
// get existing marker
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
// is this a new marker (i.e. nil)?
if view == nil {
view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")
}
// set subtitle to show without being selected
view?.subtitleVisibility = .visible
// just for fun, show green markers where subtitles exist; red otherwise
if let _ = annotation.subtitle! {
view?.markerTintColor = UIColor.green
} else {
view?.markerTintColor = UIColor.red
}
return view
}