distinction between user annotation an other annotations
please i have recently made a Map which suppose to show the user location (green pin color) and others annotations for service stations (red pins color), for the moment, i am able to show all annotations with the same color and i still unable to开发者_StackOverflow社区 tell the MKMapView
delegate how to make difference between two pin types and so assign to each type the right title to show.
this is my MKAnnotationView
method :
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
[annotationView setAnimatesDrop:YES];
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
and for my class which implement the MKAnnotation
delegate i have this method :
-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate
{
if ((self = [super init])) {
_enseigneDeLaStation = [enseigneDeLaStation copy];
_distanceVersLaStation = [distanceVersLaStation copy];
_coordinate = coordinate;
}
return self;
}
thx in advance for your help :)
EDIT i try to make the user location annotation like this, please if i am wrong correct me :
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
EDIT 2 Hi again, i try to make one annotation to make sure it even call the annotation method so imake this simple example :
latitudeOfUserLocation=43.2923;
longitudeOfUserLocation=5.45427;
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"coucou" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
MKCoordinateRegion region={location2D,span};
[mapView setRegion:region];
when i run the app, i see the region zoomed well which go with the 43.2923/5.45427 but i don't see the annotation, why it's not calling the annotation method,i can't move on since it's not showing the user annotation, please help, thx in advance :)
EDIT 3
Hi, i assumed that this has always relation with making difference between user and station pins, so i have declared a var codeColor
, set it to 1 (the type of the user) when invoking the annotations and set it to 2(station type) when calling the annotations for the stations :
-(void)viewWillAppear:(BOOL)animated
{
codeColor=1;//it's the first type, the user one
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are right here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen;
[mapView addAnnotation:annotation];
}
and :
-(void)requestFinished:(ASIHTTPRequest *)request
{ codeColor=2;
[MBProgressHUD hideHUDForView:self.view animated:YES];
MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorPurple;
[mapView addAnnotation:annotation];
}
and for the annotations method :
if (codeColor==2) {
annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
now when testing for the first time, all is ok, the user annotation without button and the station one is with the button, but when i move from the view and i came back, all annotations are with buttons, even the user one. could you please help me there, thx in advance :)
One simple way is to add a pinColor property to your MyLocation class:
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
When creating the annotation, set the pinColor:
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen; //or red or whatever
[mapView addAnnotation:annotation];
and in the viewForAnnotation method:
//...
annotationView.canShowCallout = YES;
annotationView.pinColor = ((MyLocation *)annotation).pinColor;
精彩评论