Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with Annotation Views disappearing on double tap zoom
    primarykey
    data
    text
    <p>I've ran into a problem with annotation views in the MapKit on the iPhone. I manage to draw custom annotation views on the map - no problem there. I even manage to redraw them after dragging or zooming. However, there are cases where the redrawing does not work: an example would be double-tap zoom.</p> <p>I attach some code where I draw a few rectangles at specific locations on the map, and when I zoom using a two finger gesture, everything works fine (i.e. the rectangles are redrawn). However, when I double tap, the rectangles disappear. What's even stranger is that all methods get called in the order that they should, and in the end, even the drawRect gets called - but the rectangles are not drawn.</p> <p>So here's the code, please try for yourself - two finger zooming works, but double-tap zooming doesn't:</p> <p>PlaygroundViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface PlaygroundViewController : UIViewController &lt;MKMapViewDelegate&gt;{ MKMapView *mapView_; NSMutableDictionary* myViews_; } @end </code></pre> <p>PlaygroundViewController.m</p> <pre><code>#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 &lt; 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 &lt;MKAnnotation&gt;)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]; } </code></pre> <p>Territory.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface Territory : NSObject &lt;MKAnnotation&gt; { float latitude_; float longitude_; NSString* territoryID_; } @property (nonatomic) float latitude_; @property (nonatomic) float longitude_; @property (nonatomic, retain) NSString* territoryID_; @end </code></pre> <p>Territory.m</p> <pre><code>#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 </code></pre> <p>TerritoryView.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface TerritoryView : MKAnnotationView { } - (id)initWithAnnotation:(id &lt;MKAnnotation&gt;)annotation reuseIdentifier:(NSString *)reuseIdentifier; - (void)initRedraw; @end </code></pre> <p>TerritoryView.m</p> <pre><code>#import "TerritoryView.h" @implementation TerritoryView - (id)initWithAnnotation:(id &lt;MKAnnotation&gt;)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 </code></pre> <p>Any help is appreciated. Here's the zipped project: <a href="http://www.salathe.com/docroot/Playground.zip" rel="nofollow noreferrer">link</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload