objective c - Custom curent location Annotation pin not updating with core location -
trying add custom pin current location, location not update. after setting setshowsuserlocation = yes;
- (id)initwithannotation:(id <mkannotation>)annotation reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithannotation:annotation reuseidentifier:reuseidentifier]; if ([[annotation title] isequaltostring:@"current location"]) { self.image = [uiimage imagenamed:[nsstring stringwithformat:@"cursor_%i.png", [[session instance].current_option cursorvalue]+1]]; }
however, if set return nil;
works fine, lose custom image. want work. appreciated.
as you've seen setshowsuserlocation flag shows current location using default blue bubble.
what need here listen location updates phone , manually reposition annotation yourself. can creating cllocationmanager instance , remove , replace annotation whenever location manager notifies delegate of update:
- (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { // update annotation position here }
to reposition coordinates, have class, placemark, conforms protocol mkannotation:
//--- .h --- #import <foundation/foundation.h> #import <mapkit/mapkit.h> @interface placemark : nsobject <mkannotation> { } @property (nonatomic, readonly) cllocationcoordinate2d coordinate; @property (nonatomic, retain) nsstring *strsubtitle; @property (nonatomic, retain) nsstring *strtitle; -(id)initwithcoordinate:(cllocationcoordinate2d) coordinate; - (nsstring *)subtitle; - (nsstring *)title; @end //--- .m --- @implementation placemark @synthesize coordinate; @synthesize strsubtitle; @synthesize strtitle; - (nsstring *)subtitle{ return self.strsubtitle; } - (nsstring *)title{ return self.strtitle; } -(id)initwithcoordinate:(cllocationcoordinate2d) c { self.coordinate = c; [super init]; return self; } @end
then in mapview controller place annotation with:
- (void) setplacemarkwithtitle:(nsstring *) title andsubtitle:(nsstring *) subtitle forlocation: (cllocationcoordinate2d) location { //remove pins there... nsarray *pins = [mapview annotations]; (int = 0; i<[pins count]; i++) { [mapview removeannotation:[pins objectatindex:i]]; } placemark *placemark=[[placemark alloc] initwithcoordinate:location]; placemark.strtitle = title; placemark.strsubtitle = subtitle; [mapview addannotation:placemark]; [self setspan]; //a custom method ensures map centered on annotation }
Comments
Post a Comment