1

I would like to use my own Image vor the Annotations i have in my Map. But I only get the default pins.

Here is my 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;

I can´t find what is wrong with it. The Image is found an not nil.The delegate is set.The protocol is there , the method gets called.

Here is the whole code involved in adding the annotations. "drawGamers" is the entry point and gets called by the networkhandling class.

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

I can't find where the problem is.

1
  • whether you set mapview delegate Jan 7, 2013 at 7:11

2 Answers 2

0

In viewForAnnotation, the annotationView variable is declared twice.

First at the top and then again inside the if. So the setting inside the if has no effect on the variable declared outside.

The return is done on the outside variable which stays nil.

Don't re-declare the variable inside the if -- just set it.

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

This may help you in solving your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.