发生的情况是我在搜索字段中输入地址并单击“开始”。找到位置并放大地图。我单击图钉图像,标题气泡弹出并显示标题。
我创建了一个属性“pin”作为对 title 属性的引用。拖动到新位置完成后,我使用对新位置的引用设置属性标题标签。在 NSLog 中,该属性显示为已更改。但是当我点击拖动到新位置并且拖动结束时,标题属性的相同原始值保持不变。
我看过很多关于类似事情的问题,但没有任何效果。
这是我的 GO 按钮方法:
@IBAction func didTapGoButton(sender: UIButton) {
self.spinnerContainer.hidden = false
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(searchField.text, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
var region = self.mapView.region as MKCoordinateRegion
region.center = placemark.location.coordinate
region.span.longitudeDelta = 0.0144927536
region.span.latitudeDelta = 0.0144927536
self.mapView.zoomEnabled = true
self.mapView.scrollEnabled = true
let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = "\(placemark.name), \(placemark.locality), \(placemark.country)"
self.spinnerContainer.hidden = true
self.mapView.addAnnotation(pa)
self.pin = pa
self.mapView.setRegion(region, animated: true)
self.annotationTitle = pa.title
self.searchField.text = ""
//self.mapView.selectAnnotation(pa, animated: true)
var newLocation = CLLocation(latitude: pa.coordinate.latitude, longitude: pa.coordinate.longitude)
var geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(newLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
// let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = "\(placemark.name), \(placemark.locality), \(placemark.administrativeArea), \(placemark.country), \(placemark.postalCode)"
NSLog("\(pa.title)")
self.addressFromCoordinates.text = pa.title
self.noAddressLabel.hidden = true
}
})
}
})
}
我对动画的看法:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let reuseId = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if pin == nil {
NSLog("PIN NIL")
pin = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pin.image = UIImage(named: "pin")
pin.draggable = true
pin.canShowCallout = true
}
else
{
NSLog("PIN NOT NIL")
pin.annotation = annotation
}
return pin;
}
我的 didChangeDragState 方法:
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == MKAnnotationViewDragState.Starting
{
view.dragState = MKAnnotationViewDragState.Dragging
}
else if newState == MKAnnotationViewDragState.Ending || newState == MKAnnotationViewDragState.Canceling
{
view.dragState = MKAnnotationViewDragState.None
var newLocation = CLLocation(latitude: self.pin.coordinate.latitude, longitude: self.pin.coordinate.longitude)
var geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(newLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = "\(placemark.name), \(placemark.locality), \(placemark.administrativeArea), \(placemark.country), \(placemark.postalCode)"
self.annotationTitle = pa.title
self.addressFromCoordinates.text = pa.title
self.editAddressButton.hidden = false
self.noAddressLabel.hidden = true
}
})
NSLog("\(self.pin.coordinate.latitude), \(self.pin.coordinate.longitude)")
}
}
拖动后是否需要对地图视图进行某种刷新?
谢谢你的时间