Partage
  • Partager sur Facebook
  • Partager sur Twitter

[iOS] Detecter un clic sur une annotation (pin, MKMap) ?

Debutant en Objective-C

Sujet résolu
    25 octobre 2012 à 13:30:42

    Bonjour,

    Apres avoir cherché sur le net, je me tourne vers vous pour m'aider à trouver la solution à mon problème.

    J'ai une map avec plusieurs annotations sur ma map, et ce que j'essaie de faire est de détecter un clic sur une annotation. Par exemple si je clique sur une annotation, faire en sorte que la bulle au dessus s'affiche.

    Quelqu'un peu m'aider pour ça?

    Merci
    • Partager sur Facebook
    • Partager sur Twitter
      25 octobre 2012 à 14:20:11

      Après avoir setter le delegate de ta mapView, tu dois déclarer la methode:
      - (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
          MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation"];
          if (pin == nil) {
              pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation"] autorelease];
          }
          else {
              pin.annotation = annotation;
          }
          pin.canShowCallout = YES;
      
      //Ensuite tu peux customize ta MKAnnotationView
      }
      
      • Partager sur Facebook
      • Partager sur Twitter
        25 octobre 2012 à 14:30:25

        Merci pour ta reponse.

        Je suis débutant en Objective-C, et je comprends la notion de delegate, mais j'avoue que ca reste encore un peu flou pour moi.

        Dans ce cas la, tu voudrais que je rajoute un délégate dans mes attributs et que je l'initialise??
        Jcomprends pas très bien ... :s

        J'ai fait ca :
        map.delegate = self;

        Et pourtant, aucune bulle apparait :S

        Voici mon .h de ma vue principale :
        #import <UIKit/UIKit.h>
        #import "XMLParser.h"
        #import <MapKit/MKMapView.h>
        
        @interface NetTestViewController : UIViewController <MKMapViewDelegate, XMLParserDelegate>
        {
            int         parseType;
            XMLParser *parser; // DELEGATE Pour le PARSING
            IBOutlet MKMapView *map;
        }
        
        @property (retain, nonatomic) IBOutlet CLLocationManager *locationManager;
        - (void) xmlParserdidFinishParsing;
        - (void) putPinsAfterParsing;
        @end
        


        Voici mon .m avec les fonctions concernant les maps :
        - (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
            MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: @"annotation"];
            if (pin == nil) {
                pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation"] autorelease];
            }
            else {
                pin.annotation = annotation;
            }
            pin.canShowCallout = YES;
            
            //Ensuite tu peux customize ta MKAnnotationView
            return (pin);
        }
        
        - (void) putPinsAfterParsing {
            for (int i = 0; i < [parser.coord count]; ++i)
                NSLog(@"Premier coord = %@", [parser.coord objectAtIndex:i]);
        
            CLLocationCoordinate2D  points;
            NSMutableArray *rep = [[NSMutableArray alloc] init];
            Annotation *address;
            for (int i = 0; i < [parser.coord count]; ++i)
            {
                points.latitude = [[[parser.coord objectAtIndex:i] objectForKey:@"LATITUDE"] doubleValue];
                points.longitude = [[[parser.coord objectAtIndex:i] objectForKey:@"LONGITUDE"] doubleValue];
                address = [Annotation alloc];
                address.coordinate = points;
                address.code_adr = [[parser.coord objectAtIndex:i] objectForKey:@"CODE_ADR"];
                [rep addObject:address];
                [address release];
                //[map selectAnnotation:address animated:YES]; //(pour la bulle)
            }
            [map addAnnotations:rep];
            [rep release];
            NSLog(@"Nb of pins = %d", [map.annotations count]);
            NSLog(@"First CODE ADR = %@",[[map.annotations objectAtIndex:0] code_adr]);
        }
        


        Comme tu peux voir, je sais faire une map, positionner comme je veux des pins en fonctions d'un tableau qui comporte des coordonnees. Mais pour aller plus loin, j'ai du mal a trouver des infos sur le web.

        Peux-tu m'éclaircir s'il te plait sur le fameux delegate que je devrais avoir.

        merci
        • Partager sur Facebook
        • Partager sur Twitter
          25 octobre 2012 à 17:23:50

          Ou as tu fais map.delegate = self; ?

          Tu peux vérifier que la méthode
          - (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
          

          est bien appelée en mettant NSLog (@"le delegate est set") ou un point d'arrêt dedans.
          • Partager sur Facebook
          • Partager sur Twitter
            25 octobre 2012 à 17:41:56

            J'avais réussi au dernier moment.

            Maintenant ma bulle apparaît quand je clique sur la pin rouge.
            En fait, ça ne fonctionnait pas parce que je n'avais pas mis de titre!!

            Maintenant, mission numéro 2 :
            Customizer la bulle! Arf....

            Merci à toi en tout cas ;)
            • Partager sur Facebook
            • Partager sur Twitter

            [iOS] Detecter un clic sur une annotation (pin, MKMap) ?

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown