MapKit en Swift, Parte 2
core-location cllocationmanager (2)
El método de delegado calloutAccessoryControlTapped
se debe mapView(annotationView:calloutAccessoryControlTapped:)
.
No puede usar su propio nombre como pinPressed(...)
.
Esto se aplica a cualquier método delegado y está dictado por el protocolo.
Entonces debería ser:
func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
println("Disclosure Pressed!")
}
}
Estoy intentando trabajar con Map Kit en Swift. Intento mostrar el área en el mapa, un pin (MKPinAnnotationView) y la posición actual. Todos se muestran bien. Intento agregar el botón de divulgación e interceptar el tapping sobre él. Se agregó el botón de divulgación, pero no funciona interceptando el tapping.
pinPressed
con el método de calloutAccessoryControlTapped
no funciona ....
Este es un código de muestra:
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()
}
}
Solo calloutAccessoryControlTapped
actualizando el método de delegado calloutAccessoryControlTapped
, porque lo intento hoy (06/09/2015) y no funciona. Este campo de entrada cambió: 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!")
}
}