首页 > 解决方案 > 如何在地图上显示标注点信息?

问题描述

当我在地图上单击 Annotation 时,我希望有关单击位置的信息出现在底部的后视图中。我使用 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)
            
        }
       
        
    }

标签: iosswiftmapkitmapkitannotation

解决方案


首先设置委托

myMap.delegate = self

然后实施

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

推荐阅读