快速将图像添加到MKMapAnnotation

问题描述

Final result

这是整个代码 导入UIKit 导入MapKit 导入CoreLocation 导入Firebase

MapViewController类:UIViewController,CLLocationManagerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate {

@IBOutlet weak var MapButton: UITabBarItem!
@IBOutlet weak var mapView: MKMapView!

按下此按钮后,用户必须拍照,并且照片必须在注释中可见

@IBAction func pluspressed(_ sender: UIButton) {
    

//保护let currLoc = locationManager.location else {return}

    currentLocation = locationManager.location
    
    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude,longitude: currentLocation.coordinate.longitude)
    mapView.addAnnotation(annotation)
    
    let vc = UIImagePickerController()
    vc.sourceType = .camera
    vc.allowsEditing = true
    vc.delegate = self
    present(vc,animated: true)
    
    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)

        guard let image = info[.editedImage] as? UIImage else {
            print("No image found")
            return
        }

        // print out the image size as a test
        print("Image sent")
    }
    
    func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation.isKind(of: MKUserLocation.self) {  //Handle user location annotation..
                return nil  //Default is to let the system handle it.
            }

            if !annotation.isKind(of: ImageAnnotation.self) {  //Handle non-ImageAnnotations..
                var pinAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "DefaultPinView")
                if pinAnnotationView == nil {
                    pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: "DefaultPinView")
                }
                return pinAnnotationView
            }

            //Handle ImageAnnotations..
            var view: ImageAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "imageAnnotation") as? ImageAnnotationView
            if view == nil {
                view = ImageAnnotationView(annotation: annotation,reuseIdentifier: "imageAnnotation")
            }

            let annotation = annotation as! ImageAnnotation
            view?.image = annotation.image
            view?.annotation = annotation

            return view
        }
    
}

func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {

//保护位置= location.last其他{返回} //让center = CLLocationCoordinate2D(纬度:location.coordinate.latitude,经度:location.coordinate.longitude) // let region = MKCoordinateRegion.init(center:center,latitudinalMeters:regionInMeters,portraitMeters:regionInMeters) // mapView.setRegion(region,animation:true) }

func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
    checkLocationAuthorization()
}

}

class ImageAnnotation:NSObject,MKAnnotation { var座标:CLLocationCoordinate2D var image:UIImage? var colour:UIColor?

override init() {
    self.coordinate = CLLocationCoordinate2D()
    self.image = ///// Somehow here must go the image taken by camera from the code above
    self.colour = UIColor.systemGreen
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)