首页 > 解决方案 > 添加带有点击手势的图钉时,我的图钉会显示在其他地方。

问题描述

如您所见,一切正常,但由于某种原因,当我将图钉添加到地图时,纬度和经度显示在不同的部分。

不知道还能做什么,所以这是我最后的手段..

这只是片段

在这里,我正在启动手势识别器

    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesturer:)))
    mapview?.addGestureRecognizer(longGesture)

添加注释

func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D) {

    let annotation2 = MKPointAnnotation()
    annotation2.coordinate = pointedCoordinate
    annotation2.title = text.text
    mapview?.addAnnotation(annotation2)
    print("\(text.text)annotation")

}

以及在数据库和地图上完成的工作以添加注释

@objc func handleLongPress (gesturer: UILongPressGestureRecognizer) {
    if gesturer.state == UIGestureRecognizer.State.began {
        print("began")
        let alertController = UIAlertController(title: "Enter Activity", message: "Enter Below", preferredStyle: UIAlertController.Style.alert)

        alertController.addTextField { (textfield: UITextField) in
            textfield.placeholder = "activity here"
        }
        alertController.addAction(UIAlertAction(title: "Add", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction) in

            if let alertTextfield  = alertController.textFields?.first, alertTextfield.text != nil {
                //                    print("text here\(String(describing: alertTextfield.text))")
                //                    self.text = alertTextfield
                DispatchQueue.main.async {
                    print("text here\(String(describing: alertTextfield.text))")
                    self.text = alertTextfield
                    let touchPoint : CGPoint = gesturer.location(in: self.mapview)
                    let newCoordinate: CLLocationCoordinate2D = (self.mapview?.convert(touchPoint, toCoordinateFrom: self.mapview))!
                    self.myPoints.append(newCoordinate)
                    print(newCoordinate.latitude, newCoordinate.longitude)
                    self.userpinning = newCoordinate
                    self.addAnnotationOnLocation(pointedCoordinate: newCoordinate)

                    guard let uid = Auth.auth().currentUser?.uid else {return}
                    let ref = Database.database().reference().child("Map1").child(uid)
                    let values  = ["lat": self.userpinning.latitude, "long": self.userpinning.latitude, "Captio": self.text.text, "CreatingDate": Date().timeIntervalSince1970] as [String: Any]
                    let mapref = ref.childByAutoId()
                    mapref.updateChildValues(values, withCompletionBlock: { (err, ref) in
                        print("err", err)
                    })
                }
            }
        }))

        alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: { (_) in
            alertController.dismiss(animated: true, completion: nil)
        }))
        self.present(alertController, animated: true, completion: nil)

    }
}

标签: iosmapkitmapkitannotation

解决方案


推荐阅读