【发布时间】:2015-06-15 22:17:45
【问题描述】:
我在用户使用此代码触摸的地方添加了一个图钉:
func addPin(tap: UITapGestureRecognizer) {
if (tap.state == UIGestureRecognizerState.Ended) {
var coordinate = mapView.convertPoint(tap.locationInView(mapView), toCoordinateFromView: mapView)
let address = addressAnnotationLogic.createWithCoordinate(coordinate)
mapView.addAnnotation(address)
routeLogic.addAddressAnnotation(address, toRoute: currentRoute!)
// reverse geocode
let pinLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(pinLocation!, completionHandler: {
(placemarks, error) -> Void in
if error != nil {
println("Reverse geocoder failed with error " + error.localizedDescription)
}
if placemarks.count > 0 {
let topResult = placemarks[0] as? CLPlacemark
self.addressAnnotationLogic.updateAnnotation(address, withPlacemark: topResult!)
}
})
}
}
我的 addressAnnotationLogic 只是创建了一个支持 NSManagedObjectModel 来保存它,而我的 routeLogic 只是将它添加到另一个 NSManagedObjectModel 的路由中。我的委托方法非常标准。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is AddressAnnotation {
var annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "SimplePinIdentifier")
annotationView.enabled = true
annotationView.animatesDrop = true
annotationView.draggable = false
annotationView.pinColor = MKPinAnnotationColor.Red
annotationView.canShowCallout = true
return annotationView
}
return nil
}
添加第一个图钉后,如果我再次触摸屏幕,由于某种原因,最初的图钉只会移动一点点,这看起来不正确。这更令人沮丧,因为后来我在点之间绘制了一条 MKPolyline,然后引脚会稍微移动,使多义线看起来不正确。这是关于什么的?为什么引脚已经添加到 MKMapView 后会移动一点点?
【问题讨论】:
-
你在模拟器和设备上都测试过吗?
-
@Skoua 是的,我已经在设备上尝试过了,同样的事情也发生了。这似乎有点奇怪。
-
触摸移动多少重要吗?比如,在模拟器中,如果你只点一下而不移动,还会发生吗?
-
@BradzTech 不,我只需触摸屏幕上的任何位置,图钉就会移动一点点。
-
@Crystal 感谢您提供赏金,您也可以接受答案吗?
标签: ios swift mapkit core-location