2012-03-09 32 views
5

Quiero agregar una imagen personalizada a mis anotaciones en el mapa. Y he hecho la siguiente MapAnnotationView personalizado:IOS: Agregar imagen a MKAnnotationview personalizada

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
@class POI; 

@interface MapAnnotation : MKAnnotationView <MKAnnotation > 

@property (nonatomic) CGFloat lat; 
@property (nonatomic) CGFloat lon; 
@property (nonatomic) CGFloat altitude; 
@property (nonatomic, copy) NSString * title; 
@property (nonatomic, copy) NSString * subtitle; 
@property (nonatomic,retain) NSString *source; 
@property (nonatomic,retain) UIImage *image; 

@end 

@implementation MapAnnotation 
@synthesize coordinate; 
@synthesize lat=_lat,lon=_lon,altitude= _altitude; 
@synthesize subtitle= _subtitle, title= _title, source=_source, image =_img; 


- (CLLocationCoordinate2D)coordinate;{ 
    CLLocationCoordinate2D position; 
    if (_lat != 0.0 && _lon != 0.0) { 
     position.latitude = _lat; 
     position.longitude = _lon; 

    }else { 
     position.latitude=0.0; 
     position.longitude=0.0; 
    } 

    return position; 
} 

@end 

-(void) mapDataToMapAnnotations{ 

    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10]; 
    for (id annotation in _map.annotations) 
     if (annotation != _map.userLocation) 
      [toRemove addObject:annotation]; 
    [_map removeAnnotations:toRemove]; 

    [_data removeAllObjects]; 

    [_data addObjectsFromArray:[UDdelegate naturArray]]; 


    if(_data != nil){ 
     MapAnnotation * tmpPlace; 
     //for(NSDictionary * poi in _data){ 


     for(POI* poi in _data){ 

      tmpPlace = [[MapAnnotation alloc]init]; 

      tmpPlace.title = [poi title]; 
      tmpPlace.lat = [poi lat]; 
      tmpPlace.lon = [poi lon]; 
      tmpPlace.subtitle = [poi dist]; 
      tmpPlace.image = [poi poiIcon]; 

      [self.map addAnnotation:tmpPlace]; 
      [_map setNeedsLayout]; 
     } 
    } 
} 

El problema es que los pines es la norma redPin .... Estoy seguro de que los iconos no es nulo, se han comprobado para eso.

Gracias

Respuesta

11

usted tiene que servir el método delegado MapKit mapView:viewForAnnotation: con una vista personalizada.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease]; 
    } 

    annotationView.image = [UIImage imageNamed:@"pin_image.png"]; 
    annotationView.annotation = annotation; 

    return annotationView; 
} 

para encapsular más se debe crear una vista de anotación personalizada igual que lo hizo y servir el método anterior delegado con su clase.

Te aconsejo cambiar el nombre de la clase MapAnnotation porque es confuso. También hay Anotaciones en iOS que son los titulares de los datos para esas vistas de anotación. Para resolver esto, preferiría escribir el tipo de la clase heredada, en este caso MKAnnotationView al final de su clase personalizada. Por ejemplo CustomPinAnnotationView.

+0

En mi caso, el método delegado anterior no se llama, incluso he agregado el delegado . (y también se le asigna como, mapView.delegate = self;) – stack

Cuestiones relacionadas