ios - Swift Use AlertController to add annotation on mapView -
i'm trying use alert controller textfield
create mapview
annotation using textfield
title. can print constants i've created annotation doesn't work. when use annotation code outside alertaction
works fine, can't textfield
input. doing wrong?
override func viewdidload() { super.viewdidload() let uilpgr = uilongpressgesturerecognizer(target: self, action: #selector(viewcontroller.longpress(gesturerecognizer:))) uilpgr.minimumpressduration = 2 map.addgesturerecognizer(uilpgr) } func longpress(gesturerecognizer: uigesturerecognizer) { let alert = uialertcontroller(title: "new place", message: "enter name", preferredstyle: uialertcontrollerstyle.alert) alert.addtextfield { (textfield: uitextfield) in textfield.placeholder = "name" } let okaction = uialertaction(title: "ok", style: uialertactionstyle.default) { (uialertaction) in if let tempplace = alert.textfields?[0].text { let place = tempplace let touchpoint = gesturerecognizer.location(in: self.map) let coordinate = self.map.convert(touchpoint, tocoordinatefrom: self.map) let annotation = mkpointannotation() let latitude = coordinate.latitude let longitude = coordinate.longitude annotation.title = place annotation.subtitle = "lat " + (string(format: "%.2f", latitude) + " lon " + string(format: "%.2f", longitude)) annotation.coordinate = coordinate self.map.addannotation(annotation) } } let cancelaction = uialertaction(title: "cancel", style: uialertactionstyle.cancel) { (uialertaction) in } alert.addaction(okaction) alert.addaction(cancelaction) present(alert, animated: true, completion: nil) }
just store touch points before uialertcontroller presentation , use in uialteraction because when alert popover/present while longpress cannot able save/get touch point why touch points inside uialteraction 0,0.
func longpress(gesturerecognizer: uigesturerecognizer) { let touchpointtemp = gesturerecognizer.location(in: self.map_home) let alert = uialertcontroller(title: "new place", message: "enter name", preferredstyle: uialertcontrollerstyle.alert) alert.addtextfield { (textfield: uitextfield) in textfield.placeholder = "name" } let okaction = uialertaction(title: "ok", style: uialertactionstyle.default) { (uialertaction) in if let tempplace = alert.textfields?[0].text { let place = tempplace let touchpoint = touchpointtemp let coordinate = self.map_home.convert(touchpoint, tocoordinatefrom: self.map_home) print(coordinate) let annotation = mkpointannotation() let latitude = coordinate.latitude let longitude = coordinate.longitude annotation.title = place annotation.subtitle = "lat " + (string(format: "%.2f", latitude) + " lon " + string(format: "%.2f", longitude)) annotation.coordinate = coordinate self.map_home.addannotation(annotation) } } let cancelaction = uialertaction(title: "cancel", style: uialertactionstyle.cancel) { (uialertaction) in } alert.addaction(okaction) alert.addaction(cancelaction) present(alert, animated: true, completion: nil) }
Comments
Post a Comment