J'ai rencontré un problème avec les vues d'annotations dans le MapKit de l'iPhone. Je parviens à dessiner des vues d'annotations personnalisées sur la carte - pas de problème là-bas. Je parviens même à les redessiner après avoir fait glisser ou zoomer. Cependant, il y a des cas où le redessin ne fonctionne pas: un exemple serait un double-clic sur le zoom.Problème avec les vues d'annotation disparaissant lors d'un double-clic sur le zoom
Je joins un code où je dessine quelques rectangles à des endroits spécifiques sur la carte, et quand j'objecte en utilisant un geste à deux doigts, tout fonctionne bien (c'est-à-dire que les rectangles sont redessinés). Cependant, quand je tape deux fois, les rectangles disparaissent. Ce qui est encore plus étrange, c'est que toutes les méthodes sont appelées dans l'ordre qu'elles devraient, et à la fin, même le drawRect est appelé - mais les rectangles ne sont pas dessinés.
Alors, voici le code, s'il vous plaît essayer pour vous-même - deux œuvres zoom doigt, mais zooming double tap ne:
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
Toute aide est appréciée. Voici le projet zippé: link
Veuillez poster le projet compressé. –
ok, ajouté un lien vers le projet zippé à la fin de la poste. –