我想使用我自己的Image vor我在地图中的注释。但我只得到默认的针脚。
这是我的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”是入口点,由网络处理类调用。
-(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];
}
}
}
我无法找到问题所在。
答案 0 :(得分:0)
在viewForAnnotation
中,annotationView
变量被声明两次。
首先在顶部,然后再在if
内。因此if
内的设置对外部声明的变量没有影响。
return
是在外部变量上完成的,该变量保持nil
。
不要在if
内重新声明变量 - 只需设置它。
答案 1 :(得分:0)
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
MKAnnotationView* annotationView = nil;
if ([annotation isKindOfClass:[PlayerLocation class]])
{
PlayerLocation* annotation2 = (PlayerLocation*)annotation;
static NSString *identifier = @"MyView";
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
NSString* path = [[NSBundle mainBundle] pathForResource:@"MKAnnotation" ofType:@"png"];
UIImage* im = [[UIImage alloc] initWithContentsOfFile:path];
annotationView.image= im;//you can set your image here.
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];
}
}
}
这可以帮助您解决问题。