【问题标题】:How can I show the annotation point information on the map?如何在地图上显示标注点信息?
【发布时间】:2021-08-18 19:25:00
【问题描述】:

当我在地图上点击 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)
            
        }
       
        
    }

【问题讨论】:

    标签: ios swift mapkit mapkitannotation


    【解决方案1】:

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

    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
        }
        
    }
    

    【讨论】:

      【解决方案2】:

      首先设置委托

      myMap.delegate = self
      

      然后实施

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

      【讨论】:

        猜你喜欢
        • 2023-03-30
        • 2019-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        相关资源
        最近更新 更多