如何在地图上显示标注点信息?

How can I show the annotation point information on the map?

当我点击地图上的注释时,我希望点击位置的信息出现在 bottom.I 使用 Mapkit 创建地图的后视图中

注释数组

  let annotationLocations = [
        
        ["title":"X vet Clinic","latitude":39.895177 , "longitude":32.838194],
        ["title":"Y Vet Clinic","latitude": 39.894749, "longitude":32.841074],
        ["title":"Z Vet Clinic","latitude":  39.893615, "longitude":32.841476]
      
    ]

有了这个功能,我可以在地图上显示上面经纬度指定的位置

 func createAnnotations(locations: [[String: Any]])
    {
        
        for location in locations{
           let annotations = MKPointAnnotation()
            annotations.title = location["title"] as? String
         
            annotations.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! CLLocationDegrees, longitude:  location["longitude"] as! CLLocationDegrees)
            myMap.addAnnotation(annotations)
            
        }
       
        
    }

首先设置委托

myMap.delegate = self

然后实施

func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
   let ann = view.annotation as! MKPointAnnotation
   // use ann
}

您可以将注释子类化并将所需数据放入其中;

class VetClinicAnnotation: MKPointAnnotation {
    
    let name: String
    let address: String
    let image: UIImage
    
    init(with name: String, address: String, image: UIImage) {
        self.name = name
        self.address = address
        self.image = image
    }
    
}

然后您可以从您的地图视图委托中获取注释信息;

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
    if let annotation = view.annotation as? VetClinicAnnotation {
        // Use the annotation data
    }
    
}