微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

地图上的自定义注释

如何解决地图上的自定义注释

我有一个应用程序,用户按下按钮并拍照,然后该照片进入ImageAnnotation类,并且必须将其添加到地图上,但是出现这样的错误:“在隐式展开可选控件时意外找到”

仅此错误会阻止该应用正常运行,如果您能帮助我,我将非常感谢

这是代码

extension MapViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true,completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    picker.dismiss(animated: true,completion: nil)
    
    guard let ecoImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
    
    guard let currentLocation = locationManager.location else { return }

    let pin = ImageAnnotation(coordinate: CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude,longitude: currentLocation.coordinate.longitude),image: ecoImage,color: UIColor.systemGreen)
}

}

这是必须为地图创建自定义注释的类

class ImageAnnotation : NSObject,MKAnnotation {
var coordinate: CLLocationCoordinate2D
var imageEco: UIImage?
var color: UIColor?
var imageView: UIImageView!

init(coordinate: CLLocationCoordinate2D,image: UIImage,color: UIColor) {
    self.coordinate = coordinate
    imageEco = image
    self.color = color
    imageView.image = image
    imageView.frame = CGRect(x: 0,y: 0,width: 20,height: 20)
    self.imageView = UIImageView(frame: CGRect(x: 0,height: 20))
    imageView.addSubview(self.imageView)

    self.imageView.layer.cornerRadius = 25
    self.imageView.layer.masksToBounds = true
}

}

Here is the final result that must be implemented

解决方法

您在说imageView.image = image。但是imageViewnil。所以你崩溃了。没什么大惊喜。

从更大的角度看,您似乎无法理解注释和注释视图之间的区别。注释只是消息载体:它可以说出坐标是什么,也许应该显示什么图像,仅此而已。地图视图代表的工作是根据需要创建相应的 annotation视图,而 是您要处理 view 的地方,并且可以安排它显示您的圆形图像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?