I would like to work with an annotation once it's clicked. I've looked it up on the documentation of Apple and I did googled but I can not find why this is any different than how it should be done.
Basically; I don't get a println("Pin clicked");
Why not?
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
{
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
println("Pin clicked");
}
func setAnnotationPinOnMap(annotation : NSManagedObject)
{
var subject: AnyObject? = annotation.valueForKey("subject")
var desc: AnyObject? = annotation.valueForKey("desc")
var langitude: AnyObject? = annotation.valueForKey("langitude")
var longitude: AnyObject? = annotation.valueForKey("longitude")
println(annotation);
let pin = MKPointAnnotation()
let location = CLLocationCoordinate2D(
latitude: longitude as CLLocationDegrees,
longitude: langitude as CLLocationDegrees
)
println(location.longitude, location.latitude)
pin.setCoordinate(location)
pin.title = subject as String!
pin.subtitle = desc as String!
println( pin.coordinate.longitude)
viewForAnnotation(pin);
mapView.addAnnotation(pin)
}
I have imported map kit and included the map view delegate.
In the latest version of Swift, the method declaration has changed from this:
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
to this:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
In my case the problem was that i was setting the property canShowCallout in YES, so if you set this property in YES
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
isn't called. when i removed this property the method was called.
annotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES; //try setting to NO or don't set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With