我想使用我自己的图像或地图中的注释。但我只得到默认引脚。
这是我的 mapView:ForAnnotation: 方法
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”是入口点,由网络处理类调用。
-(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];
}
}
}
我找不到问题出在哪里。