【问题标题】:My annonation isn't visible when I add it in the viewDidLoad of my app当我将它添加到我的应用程序的 viewDidLoad 中时,我的 annonation 不可见
【发布时间】:2017-04-05 05:33:19
【问题描述】:

我正在开发一个应用程序,用户必须在地图上标记一个位置,然后保存它。然后我的应用程序将其存档,当用户想要查看该位置时,应用程序应该检索该位置的坐标,并在坐标存在时在地图上添加一个注释。这是我为显示用户选择的信息而创建的一段代码-

@IBOutlet var mapGestureRecognizer: UIGestureRecognizer!
var item: itemData?

// Item Outlets
@IBOutlet weak var itemInfoView: UIView!
@IBOutlet weak var itemNameTextField: UITextField!
@IBOutlet weak var itemDescriptionLabel: UITextView!
@IBOutlet weak var mapView: MKMapView!

// Item Location Outlets
@IBOutlet weak var itemLocationView: UIView!
@IBOutlet weak var itemLocationTextView: UITextView!

let dropPin = MKPointAnnotation()

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self

    // Data loading
    itemNameTextField.delegate = self
    itemDescriptionLabel.delegate = self
    itemLocationTextView.delegate = self

    // Press recognizer

    func mapViewFunc(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
        // configure the view

        return annotationView
    }

    if let item = item {

        itemNameTextField.text = item.itemName
        itemDescriptionLabel.text = item.itemDescription
        itemLocationTextView.text = item.itemPlace

        dropPin.coordinate = mapView.convert(item.mapPoint, toCoordinateFrom: mapView)
        dropPin.title = "Location of \(item.itemName)"
        mapView.addAnnotation(dropPin)
        print("Set the location of item pin to \(String(describing: dropPin.coordinate))")


    }

    // Styles
    itemInfoView.layer.cornerRadius = 3
    itemInfoView.layer.shadowColor =  UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor
    itemInfoView.layer.shadowOffset = CGSize(width: 0, height: 1.75)
    itemInfoView.layer.shadowRadius = 1.7
    itemInfoView.layer.shadowOpacity = 0.45

    itemLocationView.layer.cornerRadius = 3
    itemLocationView.layer.shadowColor =  UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor
    itemLocationView.layer.shadowOffset = CGSize(width: 0, height: 1.75)
    itemLocationView.layer.shadowRadius = 1.7
    itemLocationView.layer.shadowOpacity = 0.45

    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
let annotation = MKPointAnnotation()
//let point = mapView.convert(coordinate, toPointTo: overlayView)
func action(gestureRecognizer:UIGestureRecognizer){

    if let item = item {
        dropPin.coordinate = mapView.convert((item.mapPoint), toCoordinateFrom: mapView)
        dropPin.title = "Location of \(item.itemName)"
        mapView.addAnnotation(dropPin)
        print("Set the location of item pin to \(String(describing: dropPin.coordinate))")
    }

    else {
        let itemName = itemNameTextField.text
        let touchPoint = gestureRecognizer.location(in: mapView)
        let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView)
        annotation.coordinate = newCoordinates
        annotation.title = "Location of \(itemName!)"
        mapView.addAnnotation(annotation)
    }

}

@IBAction func mapLocationPress(_ sender: UILongPressGestureRecognizer) {
        action(gestureRecognizer: mapGestureRecognizer)

}

我创建的所有打印语句都有效,所以我知道应用程序确实保存了坐标,并且它确实正确地转换了它们。但是,当应用程序加载viewDidLoadif let item = item 部分中的信息时,图钉不会像预期的那样放到地图上。

有人知道为什么我的代码没有按预期的方式工作吗?谢谢!

如果您需要我的完整代码,请告诉我,我会将其放入我的问题中。

【问题讨论】:

  • 也许地图在 viewDidLoad 之后才被加载,您是否尝试在 viewDidAppear 或 viewWillAppear 中添加注释?
  • 也将func mapViewFunc 放到 viewDidLoad 之外

标签: ios swift coordinates archive cgpoint


【解决方案1】:

你需要使用

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
        // configure the view

        return annotationView
}

在您的 viewDidLoad() 函数之外

【讨论】:

  • 我将这个函数移到了我的viewDidLoad() 之外,但是当屏幕加载时图钉仍然没有出现,尽管它确实保存了坐标,并且它确实将坐标分配给引脚。会不会是别的?
  • 你在这里做什么 - print("Set the location of item pin to \(String(describing: dropPin.coordinate))")
  • 当该语句运行时,xCode 打印出Set the location of the item pin to CLLocationCoordinate2D(latitude: 73.632655333854188, longitude: -104.16040759876424)(那些是我丢弃的引脚的坐标)
  • 您是否尝试缩小地图并检查注释是否存在?
  • 哎呀。我忘了看,但是是的,图钉确实出现在地图上,但是出现在错误的位置。这很奇怪,因为现在我意识到CLLocationCoordinate2D 坐标与保存的坐标不同(这就是图钉在错误位置的原因)。为什么会这样?
【解决方案2】:

你应该把mapViewFunc放在viewDidLoad之外,但是如果你想把它留在viewDidLoad

试试

func viewDidLoad() {
    super.viewDidLoad()
    DispatchQueue.main.async {
        // add pin
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多