views:

2390

answers:

8

A custom AnnotationView is updated with new coordinates. But the problem is that it visually updates only after some manipulations with MKMapView, e.g. zooming or moving. What should I do to manually update visual position on a map?

PS. I've tried to change region to current map's region. But it does change zoom. It's strange.

[mapView setRegion:[mapView region] animated:YES];
+2  A: 

I am a little shoked after hours of research. The answer is just:

[mapView setCenterCoordinate:mapView.region.center animated:NO];

Do not ask me why, but it updates a mapview and it's what i was need.

slatvick
UPD: it does not help with SDK 3.1. Still in research.
slatvick
Doesn't work in 3.1.2 either.
bradheintz
A: 

Thank you so much for figuring this out... I have spent most of the day on this problem as well, and could not solve it, until I saw your solution !!

When coming close to giving up, I noticed that in the simulator, when doing a "pinch", all of a sudden, all the pins would nicely refresh !! At that point, I knew that everything that I was doing was probably ok, and I needed to force a refresh...

Many many thanks..

Is your code working on 3.1 properly? Could you provide sample?
slatvick
I just use the MapAnnotation class shown below (sorry about the mangled stuff, my browser can't seem to be able to post this cleanly). I just need to change the mapannoation coordinate by using the method called setWithCoordinate, and that actually moves it..
A: 

Here is the interface to MapAnnotation

// // CSMapAnnotation.h // mapLines // // Created by Craig on 5/15/09. // Copyright 2009 Craig Spitzkoff. All rights reserved. //

import

import

// types of annotations for which we will provide annotation views. typedef enum { MapAnnotationTypeStart = 0, MapAnnotationTypeEnd = 1, MapAnnotationTypeImage = 2 } MapAnnotationType;

@interface MapAnnotation : NSObject { CLLocationCoordinate2D _coordinate; MapAnnotationType annotationType; NSString title; NSString subtitle; NSString userData; NSString speed; NSString* identifier; }

@property (nonatomic, retain) NSString *speed; @property (nonatomic, retain) NSString *identifier;

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate annotationType:(MapAnnotationType) annotationType title:(NSString*)title subtitle: (NSString*) subtitle speed: (NSString *) speed identifier: (NSString *) identifier;

-(id) setWithCoordinate:(CLLocationCoordinate2D)coordinate annotationType:(MapAnnotationType) annotationType title:(NSString*)title subtitle: (NSString*) subtitle speed: (NSString *) speed identifier: (NSString *) identifier;

@property MapAnnotationType annotationType; @property (nonatomic, retain) NSString* userData;

@end

And here is the implementation

// // CSMapAnnotation.m // mapLines // // Created by Craig on 5/15/09. // Copyright 2009 Craig Spitzkoff. All rights reserved. //

import "MapAnnotation.h"

@implementation MapAnnotation

@synthesize coordinate = _coordinate; @synthesize annotationType = _annotationType; @synthesize userData = _userData; @synthesize speed; @synthesize identifier;

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate annotationType:(MapAnnotationType) annotationType title:(NSString*)title subtitle: (NSString*) subtitle speed: (NSString *) speedz identifier: (NSString *) identifierz { self = [super init]; _coordinate = coordinate; _title = [title retain]; _subtitle = [subtitle retain]; _annotationType = annotationType; speed = speedz; identifier = identifierz; return self; }

-(id) setWithCoordinate:(CLLocationCoordinate2D)coordinate annotationType:(MapAnnotationType) annotationType title:(NSString*)title subtitle: (NSString*) subtitle speed: (NSString *) speedz identifier: (NSString *) identifierz { _coordinate = coordinate; _title = [title retain]; _subtitle = [subtitle retain]; _annotationType = annotationType; speed = speedz; identifier = identifierz;

return self;

}

  • (NSString *)title { return _title; }

  • (NSString *)subtitle { return _subtitle; }

-(void) dealloc { [_title release]; [_userData release];

[super dealloc];

}

@end

A: 

Did you figure this out? I am having a similar problem.

Dave
Yeah, the previvous answer helps.
slatvick
didn't work for me. i am using SDK 3.2. http://stackoverflow.com/questions/2494810/mkmapview-viewforannotation-not-called-while-loading-more-results
Dave
A: 

MKMapView observes the coordinate property of annotations via KVO. You simply need to observe proper KVO protocol and send the annotation willChangeValueForKey: and didChangeValueForKey: with keypath of @"coordinate" before and after you update the coordinates.

Likewise title and subtitle are also observed by MKMapView. so if you update those and want the value in the callout to change automatically without any effort on your part, just do the same: call willChangeValueForKey: and didChangeValueForKey:

freespace
A: 

There's no reason you can't remove and then re-add the annotation. That's probably way more performant than moving the entire map, even if its a fake move.

Jasconius
Someone want to explain why this was downvoted? This is at the very least an alternative and I would argue it's more performant.
Jasconius
I am sure most people who found this thread had already tried this; I know I did. This causes bizarre behavior.
Oh Danny Boy
A: 

The KVO method freespace described works perfectly. Removing and readding the annotations causes bizarre UI behavior (pins vanishing, reappearing...)

A: 

The answer here is NOT to refresh the MapView or the Annotation!

the coordinate property of MKAnnotation has KVO on it. If you just add the id pointer, of the object you want on the map, to the mapview and update the coordinate property with a new location, MKMapView will do the rest for you.

As close as you can get to a free lunch!

PLG