当添加带有轻击手势的图钉时,我的图钉会显示在其他位置。

时间:2018-12-06 17:34:30

标签: ios mapkit mapkitannotation

您可以看到一切正常,但是由于某些原因,当我在地图上添加图钉时,纬度和经度会显示在不同的部分。

不知道该怎么办,所以这是我的最后选择。

这只是片段

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

    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)

    }
}

0 个答案:

没有答案