我在用户触摸此代码的位置添加了一个图钉:
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时,b / c会更加令人沮丧,然后销将稍微移动一点,从而使折线看起来不正确。有人知道这是什么吗?在将销钉添加到MKMapView之后,为什么将销钉移动一点?谢谢。
您可能应该coordinate
在您的AddressAnnotation
课程中设置该属性dynamic
。
class AddressAnnotation: NSObject, MKAnnotation {
var title = ""
// this works
dynamic var coordinate: CLLocationCoordinate2D
// this doesn't
// var coordinate: CLLocationCoordinate2D
init(_ coord:CLLocationCoordinate2D)
{
coordinate = coord
}
}
如果这不起作用,请为AddressAnnotation
该类和updateAnnotation
方法发布您的代码。
我认为是这样的:
您的注释首先会coordinate
从您的第一次点击的屏幕坐标转换而来。
然后,在您的地址解析器调用的异步完成处理程序中,调用您的updateAnnotation()
方法。
我假设你更新coordinate
你AddressAnnotation
到最近的地标在那里坐标。不幸的是,更新发生时,“地图视图”可能已经在原始位置绘制了Pin。
异步更新坐标时,地图视图不会注意到它。仅当重新绘制注释时(下次轻击提示),它才会拾取更新的坐标(因此您会看到从第一次轻击坐标到最近的地标坐标的移动)。
现在,“地图视图”实际上正试图通知其注释坐标的变化,以便它可以在新坐标处自动重绘。为此,它使用了一种称为键值观察的技术。但是,“普通” Swift属性不支持该功能。使他们dynamic
做到。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句