Swift MKMapView中注释编号内部的链接
是否有可能在MapKit中的注释中包含链接 我希望通过单击“显示更多”链接(但仅限于单击时),可以在该气泡内显示更多文本,在单击注释时显示Swift MKMapView中注释编号内部的链接,swift,mkmapview,Swift,Mkmapview,是否有可能在MapKit中的注释中包含链接 我希望通过单击“显示更多”链接(但仅限于单击时),可以在该气泡内显示更多文本,在单击注释时显示 否则,它仅显示注释的标题。我们将使用MKAnnotationView的以下子类添加注释,如下所示: class CustomAnnotation: MKPointAnnotation { var isCollapsed = true // current state // set true when user taps the link t
否则,它仅显示注释的标题。我们将使用
MKAnnotationView
的以下子类添加注释,如下所示:
class CustomAnnotation: MKPointAnnotation {
var isCollapsed = true // current state
// set true when user taps the link to expand/collapse annotation-view
var setNeedsToggle = false
}
let annotation = CustomAnnotation()
annotation.coordinate = self.mapView.centerCoordinate
annotation.title = "Annotation Title"
mapView.addAnnotation(annotation)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? CustomAnnotation else { return nil }
// Initialize AnnotationView
var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationView(withIdentifier: "AnID")
if (annotationView == nil) {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnID")
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
// Expand/Collapse Button
let rightButton = UIButton(type: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
// Description View
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
} else {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "A longer description in place to be shown when the accessory view is tapped"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let oldAnnot = view.annotation as? CustomAnnotation else { return }
let annotation = CustomAnnotation()
annotation.coordinate = oldAnnot.coordinate
annotation.title = oldAnnot.title
annotation.setNeedsToggle = true
if (oldAnnot.isCollapsed) {
annotation.isCollapsed = false
}
mapView.removeAnnotation(oldAnnot)
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
for view in views {
if let annotation = view.annotation as? CustomAnnotation, annotation.setNeedsToggle {
mapView.selectAnnotation(annotation, animated: true)
return
}
}
}
在注释的视图中
我们使用detailcalloaccessoryview
和rightcalloaccessoryview
来显示说明和切换链接,如下所示:
class CustomAnnotation: MKPointAnnotation {
var isCollapsed = true // current state
// set true when user taps the link to expand/collapse annotation-view
var setNeedsToggle = false
}
let annotation = CustomAnnotation()
annotation.coordinate = self.mapView.centerCoordinate
annotation.title = "Annotation Title"
mapView.addAnnotation(annotation)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? CustomAnnotation else { return nil }
// Initialize AnnotationView
var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationView(withIdentifier: "AnID")
if (annotationView == nil) {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnID")
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
// Expand/Collapse Button
let rightButton = UIButton(type: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
// Description View
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
} else {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "A longer description in place to be shown when the accessory view is tapped"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let oldAnnot = view.annotation as? CustomAnnotation else { return }
let annotation = CustomAnnotation()
annotation.coordinate = oldAnnot.coordinate
annotation.title = oldAnnot.title
annotation.setNeedsToggle = true
if (oldAnnot.isCollapsed) {
annotation.isCollapsed = false
}
mapView.removeAnnotation(oldAnnot)
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
for view in views {
if let annotation = view.annotation as? CustomAnnotation, annotation.setNeedsToggle {
mapView.selectAnnotation(annotation, animated: true)
return
}
}
}
calloutAccessoryControlTapped
点击链接时触发事件,因此我们展开/折叠注释视图,如下所示:
class CustomAnnotation: MKPointAnnotation {
var isCollapsed = true // current state
// set true when user taps the link to expand/collapse annotation-view
var setNeedsToggle = false
}
let annotation = CustomAnnotation()
annotation.coordinate = self.mapView.centerCoordinate
annotation.title = "Annotation Title"
mapView.addAnnotation(annotation)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? CustomAnnotation else { return nil }
// Initialize AnnotationView
var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationView(withIdentifier: "AnID")
if (annotationView == nil) {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnID")
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
// Expand/Collapse Button
let rightButton = UIButton(type: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
// Description View
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
} else {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "A longer description in place to be shown when the accessory view is tapped"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let oldAnnot = view.annotation as? CustomAnnotation else { return }
let annotation = CustomAnnotation()
annotation.coordinate = oldAnnot.coordinate
annotation.title = oldAnnot.title
annotation.setNeedsToggle = true
if (oldAnnot.isCollapsed) {
annotation.isCollapsed = false
}
mapView.removeAnnotation(oldAnnot)
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
for view in views {
if let annotation = view.annotation as? CustomAnnotation, annotation.setNeedsToggle {
mapView.selectAnnotation(annotation, animated: true)
return
}
}
}
最后,我们检查setNeedsToggle
是否为真,因此显示展开/折叠的注释视图,如下所示:
class CustomAnnotation: MKPointAnnotation {
var isCollapsed = true // current state
// set true when user taps the link to expand/collapse annotation-view
var setNeedsToggle = false
}
let annotation = CustomAnnotation()
annotation.coordinate = self.mapView.centerCoordinate
annotation.title = "Annotation Title"
mapView.addAnnotation(annotation)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? CustomAnnotation else { return nil }
// Initialize AnnotationView
var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationView(withIdentifier: "AnID")
if (annotationView == nil) {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnID")
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
// Expand/Collapse Button
let rightButton = UIButton(type: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
// Description View
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
} else {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "A longer description in place to be shown when the accessory view is tapped"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let oldAnnot = view.annotation as? CustomAnnotation else { return }
let annotation = CustomAnnotation()
annotation.coordinate = oldAnnot.coordinate
annotation.title = oldAnnot.title
annotation.setNeedsToggle = true
if (oldAnnot.isCollapsed) {
annotation.isCollapsed = false
}
mapView.removeAnnotation(oldAnnot)
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
for view in views {
if let annotation = view.annotation as? CustomAnnotation, annotation.setNeedsToggle {
mapView.selectAnnotation(annotation, animated: true)
return
}
}
}
以下是展开/折叠视图:
您可以尝试在地图工具包的注释视图中添加自定义视图。在自定义视图中,您可以放置标签或按钮。perfekt这正是我想要的