Problema con las vistas de anotación que desaparecen al hacer doble clic en el zoom

votos
1

Me encontré con un problema con las vistas de anotación en MapKit en el iPhone. Logré dibujar vistas de anotación personalizadas en el mapa, no hay problema allí. Incluso logro volver a dibujarlos después de arrastrar o hacer zoom. Sin embargo, hay casos en que el redibujado no funciona: un ejemplo sería hacer doble clic en el zoom.

Adjunto algún código donde dibujo algunos rectángulos en ubicaciones específicas en el mapa, y cuando hago zoom con un gesto de dos dedos, todo funciona bien (es decir, los rectángulos se vuelven a dibujar). Sin embargo, cuando hago doble toque, los rectángulos desaparecen. Lo que es aún más extraño es que todos los métodos se llaman en el orden en que deberían, y al final, incluso se llama a drawRect, pero los rectángulos no se dibujan.

Así que aquí está el código, por favor, pruébalo: el zoom de dos dedos funciona, pero el zoom doble no:

PlaygroundViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface PlaygroundViewController : UIViewController <MKMapViewDelegate>{
 MKMapView *mapView_;
 NSMutableDictionary* myViews_;
}

@end

PlaygroundViewController.m

#import PlaygroundViewController.h
#import Territory.h
#import TerritoryView.h

@implementation PlaygroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 mapView_=[[MKMapView alloc] initWithFrame:self.view.bounds];
 [self.view insertSubview:mapView_ atIndex:0];
 mapView_.delegate = self;
 [mapView_ setMapType:MKMapTypeStandard];
    [mapView_ setZoomEnabled:YES];
    [mapView_ setScrollEnabled:YES];
 myViews_ = [[NSMutableDictionary alloc] init];
 for (int i = 0; i < 10; i++ ) {
  Territory *territory;
  territory = [[[Territory alloc] init] autorelease];
     territory.latitude_ = 40 + i;
  territory.longitude_ = -122 + i;
  [mapView_ addAnnotation:territory];

 }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
 MKAnnotationView* territoryView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@Territory];
 if (!territoryView){
  territoryView = [[[TerritoryView alloc] initWithAnnotation:annotation reuseIdentifier:@Territory] autorelease];
  Territory* currentTerritory = (Territory*) annotation;
  [myViews_ setObject:territoryView forKey:currentTerritory.territoryID_];
 }  
    else{
  territoryView.annotation = annotation;
 }  
 return territoryView; 
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
 for (NSObject* key in [myViews_ allKeys]) {
  TerritoryView* territoryView = [myViews_ objectForKey:key];
  [territoryView initRedraw];
 }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [super dealloc];
}

Territory.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface Territory : NSObject <MKAnnotation> {
 float latitude_;
 float longitude_;
 NSString* territoryID_;
}

@property (nonatomic) float latitude_;
@property (nonatomic) float longitude_;
@property (nonatomic, retain) NSString* territoryID_;


@end

Territory.m

#import Territory.h

@implementation Territory

@synthesize latitude_;
@synthesize longitude_;
@synthesize territoryID_;


- (CLLocationCoordinate2D)coordinate {
 CLLocationCoordinate2D coord_ = {self.latitude_, self.longitude_};
 return coord_;
}

-(id) init {
 if (self = [super init]) {
  self.territoryID_ = [NSString stringWithFormat:@%p, self];
 }
 return self;
}


@end

TerritoryView.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface TerritoryView : MKAnnotationView {

}

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
- (void)initRedraw;

@end

TerritoryView.m

#import TerritoryView.h

@implementation TerritoryView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithAnnotation:annotation reuseIdentifier:@Territory]) {
  self.initRedraw;
    }
    return self;
}

- (void)initRedraw {
 self.frame = CGRectMake(0,0,40,40);
 [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
 NSLog(@in draw rect);
}

@end

Cualquier ayuda es apreciada. Aquí está el proyecto comprimido: enlace

Publicado el 03/10/2009 a las 06:32
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
0

Tenga en cuenta que el origen de la trama es en el sistema de coordenadas de su padre, por lo que establecer a cero probablemente es decirlo fuera de la pantalla. Sospecho que la razón es que alguna vez funciona en absoluto es el que se está haciendo reinicio detrás de la espalda en la mayoría de las situaciones, pero no en aquellos en los que está fallando.

reemplazar: self.frame = CGRectMake (0,0,40,40);

con: self.frame = CGRectMake (self.frame.origin.x, self.frame.origin.y, 40,40);

Respondida el 28/02/2010 a las 00:10
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more