マップにある注釈に独自の画像を使用したいと思います。しかし、デフォルトのピンしか取得できません。
これが私の mapView:ForAnnotation: Method です
MKAnnotationView* annotationView = nil;
if ([annotation isKindOfClass:[PlayerLocation class]]){
PlayerLocation* annotation2 = (PlayerLocation*)annotation;
static NSString *identifier = @"MyView";
MKAnnotationView *annotationView = (MKAnnotationView *) [mapV dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
NSString* path = [[NSBundle mainBundle] pathForResource:@"MKAnnotation" ofType:@"png"];
UIImage* im = [[UIImage alloc] initWithContentsOfFile:path];
annotationView.image= im;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//delete the flagged Locations
[self performSelectorOnMainThread:@selector(deletePin:) withObject:annotation2.name waitUntilDone:NO];
}
return annotationView;
何が悪いのかわかりません。画像が nil ではないことがわかりました。デリゲートが設定されています。プロトコルが存在し、メソッドが呼び出されます。
注釈の追加に関連するコード全体を次に示します。「drawGamers」はエントリ ポイントであり、networkhandling クラスによって呼び出されます。
-(void)drawGamers:(NSMutableArray*)locations{
for(PlayerLocation* loc in locations ){
[ self replacePin:loc.name withLocation:loc.coordinate];
}
}
-(void)replacePin:(NSString *)gamerName withLocation:(CLLocationCoordinate2D)location {
// flag Location for deletion later
for (PlayerLocation* annotation in _mapView.annotations){
if ([annotation.name isEqualToString:gamerName])
annotation.decomission = YES;
}
//add new Location
PlayerLocation *pLoc = nil;
pLoc = [[PlayerLocation alloc] initWithName:gamerName address:nil coordinate:location];
[_mapView addAnnotation:pLoc];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapV viewForAnnotation:(id <MKAnnotation>)annotation{
MKAnnotationView* annotationView = nil;
if ([annotation isKindOfClass:[PlayerLocation class]]){
PlayerLocation* annotation2 = (PlayerLocation*)annotation;
static NSString *identifier = @"MyView";
MKAnnotationView *annotationView = (MKAnnotationView *) [mapV dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
NSString* path = [[NSBundle mainBundle] pathForResource:@"MKAnnotation" ofType:@"png"];
UIImage* im = [[UIImage alloc] initWithContentsOfFile:path];
annotationView.image= im;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//delete the flagged Locations
[self performSelectorOnMainThread:@selector(deletePin:) withObject:annotation2.name waitUntilDone:NO];
}
return annotationView;
}
-(void)deletePin:(NSString *)stationCode{
for (PlayerLocation *annotation in _mapView.annotations) {
if ([annotation.name isEqualToString:stationCode]){
if (annotation.decomission==YES)
[_mapView removeAnnotation:annotation];
}
}
}
問題がどこにあるのかわかりません。