1

我想使用我自己的图像或地图中的注释。但我只得到默认引脚。

这是我的 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];
        }
    }
}

我找不到问题出在哪里。

4

2 回答 2

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];
        }
    }
}

这可以帮助您解决问题。

于 2013-04-02T04:29:44.280 回答
0

viewForAnnotation中,annotationView变量被声明了两次。

首先在顶部,然后在内部if。所以里面的设置if对外面声明的变量没有影响。

return是在保持的外部变量上完成的nil

不要在if-- 中重新声明变量,只需设置它。

于 2012-09-28T10:52:33.320 回答