TheInterestedOne
TheInterestedOne

Reputation: 768

Mapkit new annotations with pin without popup

i've just created a new app, and i've implemented a map in a view. I've created the map with some personal annotation but, when the user touch the pin, doesn't appear anything. I've tried to set the colour of the pin but nothing seem's to work. I need that the user, when touch the pin, can touch the disclosure button on the popup, to use "native maps". Now, when i touch the pin, this one become darker, but nothing else. Can someone help me? Please!!

io ne avrei bisogno, per far aprire poi un disclosure e far aprire l'app nativa mappe per creare il percorso!! Se ora tocco il pin, questo diventa più scuro ma non succede nulla! non riesco nemmeno ad agire su di loro per cambiare colore, immagini,.... ho guardato i tutorial ma non trovo l'errore!

header subclass

 #import <Foundation/Foundation.h>
    #import <MapKit/Mapkit.h>

    @interface myAnnotation : NSObject <MKAnnotation>{

        CLLocationCoordinate2D coordinate;
        NSString *titolo;
        NSString *sottotitolo;
    }

    @property(nonatomic,assign) CLLocationCoordinate2D coordinate;
    @property(nonatomic,copy) NSString *titolo;
    @property(nonatomic,copy) NSString *sottotitolo;


    @end

implementation subclass

@implementation myAnnotation

    @synthesize titolo;
    @synthesize sottotitolo;
    @synthesize coordinate;

    -init{
        return self;

    }

    @end

file .m view controller

CLLocation *userLoc = myMapView.userLocation.location;
        CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
        NSLog(@"user latitude = %f",userCoordinate.latitude);
        NSLog(@"user longitude = %f",userCoordinate.longitude);
        myMapView.delegate=self;


        NSMutableArray* annotations=[[NSMutableArray alloc] init];

        CLLocationCoordinate2D theCoordinate1;
        theCoordinate1.latitude = 45.;
        theCoordinate1.longitude = 7.;

        CLLocationCoordinate2D theCoordinate2;
        theCoordinate2.latitude = 45.;
        theCoordinate2.longitude = 12.;

        CLLocationCoordinate2D theCoordinate3;
        theCoordinate3.latitude = 45.;
        theCoordinate3.longitude = 8.;

        CLLocationCoordinate2D theCoordinate4;
        theCoordinate4.latitude = 43.;
        theCoordinate4.longitude = 7.;

        myAnnotation* myAnnotation1=[[myAnnotation alloc] init];

        myAnnotation1.coordinate=theCoordinate1;
        myAnnotation1.titolo=@"xxxx";
        myAnnotation1.sottotitolo=@"xxx";


        myAnnotation* myAnnotation2=[[myAnnotation alloc] init];

        myAnnotation2.coordinate=theCoordinate2;
        myAnnotation2.titolo=@"yyyy";
        myAnnotation2.sottotitolo=@"yyyy";

        myAnnotation* myAnnotation3=[[myAnnotation alloc] init];

        myAnnotation3.coordinate=theCoordinate3;
        myAnnotation3.titolo=@"zzz";
        myAnnotation3.sottotitolo=@"zzz";

        myAnnotation* myAnnotation4=[[myAnnotation alloc] init];

        myAnnotation4.coordinate=theCoordinate4;
        myAnnotation4.titolo=@"kkk";
        myAnnotation4.sottotitolo=@"kkk";


        [myMapView addAnnotation:myAnnotation1];
        [myMapView addAnnotation:myAnnotation2];
        [myMapView addAnnotation:myAnnotation3];
        [myMapView addAnnotation:myAnnotation4];


        [annotations addObject:myAnnotation1];
        [annotations addObject:myAnnotation2];
        [annotations addObject:myAnnotation3];
        [annotations addObject:myAnnotation4];


        NSLog(@"%d",[annotations count]);

and then this snippet to show and personalize the pin

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

Upvotes: 1

Views: 771

Answers (1)

user467105
user467105

Reputation:

The MKAnnotation protocol requires that the class responds to title and subtitle. The properties must be named exactly like that.

Although you can also have your own differently named properties for the same, the map view will not call them (it is looking for title and subtitle).

For the disclosure button, in viewForAnnotation, set the rightCalloutAccessoryView to a UIButton and respond to its tap in the calloutAccessoryControlTapped delegate method.

In that delegate method, you can access the annotation object using (myAnnotation *)view.annotation and then call openURL to open the Maps app.

Upvotes: 3

Related Questions