我第一次设置后似乎无法设置MKMapView注释标题

时间:2014-09-15 03:35:00

标签: ios swift nsstring mkmapview mkmapviewdelegate

如果我在搜索字段中键入地址,请单击“执行”。找到位置并放大地图。我点击图钉图像,弹出标题气泡并显示标题。

我创建了一个属性“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)")
        }
    }

拖动后是否需要对地图视图进行某种刷新?

感谢您的时间

1 个答案:

答案 0 :(得分:4)

didChangeDragState中,此代码:

let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = "\(placemark.name), \(placemark.locality), \(placemark.administrativeArea), \(placemark.country), \(placemark.postalCode)"

self.annotationTitle = pa.title

没有更新刚刚拖动的注释的title,因为:

  1. pa中的pa.title = ...指的是MKPointAnnotation 实例只是拖动注释的方式。 let pa = MKPointAnnotation()行会创建MKPointAnnotation的新实例。这个新实例只存在于内存本地,甚至没有添加到地图中。

  2. self.annotationTitle的更新对刚刚拖动的注释实例的title没有影响,因为annotationTitle只是您声明的一些单独的字符串变量,并且添加到地图中并且刚刚拖动的原始MKPointAnnotationannotationTitle没有任何关系(事件annotationTitle最初设置为等于注释的title didTapGoButton不会以某种方式将两个字符串“链接”在一起。)


  3. didTapGoButton中,您将保存对pin变量中实际添加(然后拖动)的注释对象的引用。此pin变量是可用于更新已拖动注释的title的引用(假设您一次只有一个注释)。

    因此,在didChangeDragState中,将上面显示的代码更改为:

    self.pin.coordinate = placemark.location.coordinate
    self.pin.title = "\(placemark.name), \(placemark.locality), \(placemark.administrativeArea), \(placemark.country), \(placemark.postalCode)"
    
    self.annotationTitle = self.pin.title
    


    但是,根据您的要求,请注意,首先不必保留对注释的引用...

    didChangeDragState中,您可以访问直接从传递给方法的view参数中拖动的注释。例如:

    let ann = view.annotation as MKPointAnnotation
    ann.coordinate = placemark.location.coordinate
    ann.title = "\(placemark.name), \(placemark.locality), \(placemark.administrativeArea), \(placemark.country), \(placemark.postalCode)"
    
    self.annotationTitle = ann.title
    


    旁注:
    拖动注释并对新位置进行反向地理编码后,地理编码坐标很可能与用户拖动注释的位置不同。发生这种情况是因为最近的地理编码地址可能稍微远离拖动注释的位置。那么在用户完成拖动之后,注释会稍微移动一下。如果您希望将注释准确保留在用户拖动的位置,请不要更新地理编码器完成块中的注释coordinate,只更新title。如果地理编码的地址距离实际坐标X米或更远,您可能需要将标题设置为“near xyz”。