在Alertcontroller tapgesturer不出现且未保存到数据库后添加注释

时间:2018-08-14 17:00:44

标签: ios firebase mapkit uialertcontroller mkannotation

一旦用户按下地图上的某处,我就会在该地图上添加一个图钉。它弹出一个警报控制器,文本显示在我的打印物中,但没有添加到地图上,也没有保存到我的数据库中。另外,如何保存数组,这样当我离开地图时它不会消失。

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesturer:)))


    mapView?.addGestureRecognizer(longGesture)


var myPoints = [CLLocationCoordinate2D]()
var myAnnotations = [CLLocation]()

@objc func handleLongPress (gesturer: UILongPressGestureRecognizer) {


    if gesturer.state == UIGestureRecognizerState.began {

    print("began")

        let alertController = UIAlertController(title: "Enter Activity", message: "Enter Below", preferredStyle: UIAlertControllerStyle.alert)

        alertController.addTextField { (textfield: UITextField) in
            textfield.placeholder = "activity here"
        }

        alertController.addAction(UIAlertAction(title: "Add", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
            if let alertTextfield  = alertController.textFields?.first, alertTextfield.text != nil {
                print("text here\(alertTextfield.text)")
                self.text = alertTextfield
            }

                let touchPoint : CGPoint = gesturer.location(in: self.mapView)
                let newCoordinate: CLLocationCoordinate2D = (self.mapView?.convert(touchPoint, toCoordinateFrom: self.mapView))!
                self.addAnnotationOnLocation(pointedCoordinate: newCoordinate)
                self.myPoints.append(newCoordinate)
                print(newCoordinate.latitude, newCoordinate.longitude)

                self.userpinning = newCoordinate

            guard let uid = self.user?.uid else {return}

                let ref = Database.database().reference().child("Map").child(uid)

            let values  = ["lat": self.userpinning.latitude, "long": self.userpinning.latitude, "activity": self.text.text] as [String: Any]
               let mapref = ref.childByAutoId()
            mapref.updateChildValues(values, withCompletionBlock: { (err, ref) in
                print("err",err)
            })

        }))


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

        self.present(alertController, animated: true, completion: nil)

    }
}




func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D) {

        let annotation2 = MKPointAnnotation()
        annotation2.coordinate = pointedCoordinate
        annotation2.title = text.text
        annotation2.subtitle = user?.uid
        mapView?.addAnnotation(annotation2)


    print("\(text.text)annotation")

}

0 个答案:

没有答案