【发布时间】:2019-08-08 07:32:39
【问题描述】:
我正在制作一个应用程序,您可以在其中通过长按将图钉添加到地图位置。但是,长按似乎在复制这些位置。这是我的代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations[0]
if activePlace == -1 {
latitude = userLocation.coordinate.latitude
longitude = userLocation.coordinate.longitude
} else {
latitude = Double(latitudePassed)!
longitude = Double(longitudePassed)!
}
let latDelta : CLLocationDegrees = 0.05
let lonDelta : CLLocationDegrees = 0.05
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)) )
uilpgr.minimumPressDuration = 2
map.addGestureRecognizer(uilpgr)
}
@objc func longpress(gestureRecognizer: UIGestureRecognizer) {
let touchpoint = gestureRecognizer.location(in: self.map)
print(touchpoint)
let coordinate = map.convert(touchpoint, toCoordinateFrom: self.map)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "New Place"
let annotationLat = coordinate.latitude
let annotationLon = coordinate.longitude
places.append(["name": annotation.title!, "latitude": String(annotationLat), "longitude": String(annotationLon)])
map.addAnnotation(annotation)
}
如您所见,我在函数开始时打印接触点,并且多次打印相同的位置 - 有时两次,有时最多 12 次。我搜索了 StackOverflow 并找不到类似的问题...任何帮助将不胜感激。
【问题讨论】:
-
为什么每次在 didUpdateLocations 中更新位置时都添加 UILongPressGestureRecognizer ?
标签: ios swift long-press uilongpressgesturerecogni