问题
My MKPointAnnotation should with this code be custom:
-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
Pin = [[MKPointAnnotation alloc] init];
Pin.title = title;
[Pin setCoordinate:Location];
[self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
return Pin;
}
-(MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>) annotation{
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if(!pinView){
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.enabled = YES;
UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
pinView.rightCalloutAccessoryView = PicButton;
}
else{
pinView.annotation = annotation;
}
return pinView;
}
Yet, for some reason, the Pin is still default, can anyone help me out here? Thanks
回答1:
You shouldn't call viewForAnnotation yourself. You should only add annotations to your map view, which will then determine which are visible at any given moment and call viewForAnnotation for you. Thus:
Given that different annotations can have different images, you really want to subclass
MKPointAnnotationand give it a property of theimageName(note, not theUIImageitself, but a name or identifier thatviewForAnnotationwill be able to use to create theUIImageitself):@interface MyAnnotation : MKPointAnnotation @property(nonatomic, strong) NSString *imageName; @end @implementation MyAnnotation @endAdd an annotation (not an annotation view) to your mapview, e.g.:
- (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName { MyAnnotation *annotation = [[MyAnnotation alloc] init]; annotation.title = title; annotation.coordinate = coordinate; annotation.imageName = imageName; [mapView addAnnotation:annotation]; return annotation; }Note, I wouldn't suggest making
Pina class ivar/property, and I'd use camelCase for variable names (start with lowercase), I'd change the method name fromsetAnnotationto something likeaddAnnotationWithTitle, etc.Make sure your controller has been specified as the delegate of the mapView;
The
viewForAnnotationwill be called for you (if the annotation is visible). It should probably be something like:- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; if ([annotation isKindOfClass:[MyAnnotation class]]) { MyAnnotation *customAnnotation = annotation; static NSString *annotationIdentifier = @"MyAnnotation"; MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if (!annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; annotationView.canShowCallout = YES; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; } else { annotationView.annotation = annotation; } annotationView.image = [UIImage imageNamed:customAnnotation.imageName]; return annotationView; } return nil; }Note, the
animatesDropis aMKPinAnnotationViewoption that you can't do with custom annotation views. But it's easy to implement one if you want it (see How can I create a custom "pin-drop" animation using MKAnnotationView?). I'd suggest you tackle the basic annotation view first, and then look at custom annotation view drop animation later.
来源:https://stackoverflow.com/questions/16507171/why-is-my-mkpointannotation-not-custom