MapKit in Swift, Part 2

2019-01-15 19:44发布

I'm trying work with Map Kit in Swift. I try to display the area on the map, one pin (MKPinAnnotationView) and the current position. All display fine. I try to add Disclosure Button and intercept tapping on it. Disclosure Button added, but does not work intercept tapping.

Function pinPressed with method calloutAccessoryControlTapped not work....

This is a sample code:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    var objectLatitude = 53.204526
    var objectLongitude = 50.111751

    var currentLatitude = 53.203715
    var currentLongitude =  50.160374

    var latDelta = 0.05
    var longDelta = 0.05

    var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
    var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
    self.mainMapView.setRegion(currentRegion, animated: true)

    var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude)
    var objectAnnotation = MKPointAnnotation()
    objectAnnotation.coordinate = objectLocation
    objectAnnotation.title = "St. George's Church"
    objectAnnotation.subtitle = "Church of the Great Martyr St. George"
    self.mainMapView.addAnnotation(objectAnnotation)
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Purple
            pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
}

func pinPressed(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-15 20:42

Just updating the calloutAccessoryControlTapped delegate method, because i try it today (09/06/2015) and not works. This input field changed: annotationView view: MKAnnotationView!

//Click on left or right button
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)
{
    let anotation = view.annotation as! MyAnnotation

    if (control == view.rightCalloutAccessoryView)
    {
        println("Button right pressed!")
    }
    else if (control == view.leftCalloutAccessoryView)
    {
        println("Button left pressed!")
    }
}
查看更多
乱世女痞
3楼-- · 2019-01-15 20:45

The calloutAccessoryControlTapped delegate method must be named mapView(annotationView:calloutAccessoryControlTapped:).

You can't use your own name like pinPressed(...).

This applies to any delegate method and is dictated by the protocol.

So it should be:

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}
查看更多
登录 后发表回答