I'm creating a function that when it's called, it will zoom in into the custom pin annotation coordinates and show its title and subtitle. The zoom part works fine, the only thing I can't do it's the title and subtitle part.
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)
}
I don't know if this is useful, but I'll also leave this part of the code:
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
}
CodePudding user response:
Select annotation after adding it to mapView. Here is the code.
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)
}