如何在调用函数时自动显示 MKPointAnnotation 标题?
How to automatically show a MKPointAnnotation title when a function is called?
我正在创建一个函数,当它被调用时,它会放大自定义图钉注释坐标并显示其标题和副标题。缩放部分很好用,我唯一做不到的是标题和副标题部分。
private func addCustomPinRectoría1() {
let pin1 = MKPointAnnotation()
pin1.coordinate = coordinatesRectoría1
pin1.title = "Rectoría"
pin1.subtitle = "Parada TigreBus"
map.addAnnotation(pin1)
func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
guard let coordinates = coordinates else {
return
}
map.removeAnnotations(map.annotations)
let selectedPin = MKPointAnnotation()
selectedPin.coordinate = coordinates
map.addAnnotation(selectedPin)
// Here is where I need to display the pin's title and subtitle automatically
map.setRegion(MKCoordinateRegion(
center: coordinates,
span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
animated: true)
}
不知道有没有用,我也把这部分代码留下来:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
var annotationView = map.dequeueReusableAnnotationView(withIdentifier: "custom")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
annotationView?.canShowCallout = true
}
else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "TigreBusParada")
annotationView?.frame.size = CGSize(width: 35, height: 100)
return annotationView
}
Select 添加到 mapView 后的注释。这是代码。
func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
guard let coordinates = coordinates else {
return
}
map.removeAnnotations(map.annotations)
let selectedPin = MKPointAnnotation()
selectedPin.coordinate = coordinates
map.addAnnotation(selectedPin)
//add this line to show the title and subtitle
map.selectAnnotation(selectedPin, animated: false)
map.setRegion(MKCoordinateRegion(
center: coordinates,
span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
animated: true)
}
我正在创建一个函数,当它被调用时,它会放大自定义图钉注释坐标并显示其标题和副标题。缩放部分很好用,我唯一做不到的是标题和副标题部分。
private func addCustomPinRectoría1() {
let pin1 = MKPointAnnotation()
pin1.coordinate = coordinatesRectoría1
pin1.title = "Rectoría"
pin1.subtitle = "Parada TigreBus"
map.addAnnotation(pin1)
func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
guard let coordinates = coordinates else {
return
}
map.removeAnnotations(map.annotations)
let selectedPin = MKPointAnnotation()
selectedPin.coordinate = coordinates
map.addAnnotation(selectedPin)
// Here is where I need to display the pin's title and subtitle automatically
map.setRegion(MKCoordinateRegion(
center: coordinates,
span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
animated: true)
}
不知道有没有用,我也把这部分代码留下来:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
var annotationView = map.dequeueReusableAnnotationView(withIdentifier: "custom")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
annotationView?.canShowCallout = true
}
else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "TigreBusParada")
annotationView?.frame.size = CGSize(width: 35, height: 100)
return annotationView
}
Select 添加到 mapView 后的注释。这是代码。
func selectAnnotation(_ vc: ContentViewController, didSelectLocationWith coordinates: CLLocationCoordinate2D?) {
guard let coordinates = coordinates else {
return
}
map.removeAnnotations(map.annotations)
let selectedPin = MKPointAnnotation()
selectedPin.coordinate = coordinates
map.addAnnotation(selectedPin)
//add this line to show the title and subtitle
map.selectAnnotation(selectedPin, animated: false)
map.setRegion(MKCoordinateRegion(
center: coordinates,
span: MKCoordinateSpan(latitudeDelta: 0.008, longitudeDelta: 0.008)),
animated: true)
}