ios - Problems Trying to Find User Location in MKMapView -
i having problems trying user's current position display on mkmapview. here relevant code:
header:
// parkingmapviewcontroller.m #import <uikit/uikit.h> #import <mapkit/mapkit.h> @interface parkingmapviewcontroller : uiviewcontroller <mkmapviewdelegate, cllocationmanagerdelegate> { mkmapview *mapview; cllocationmanager *locationmanager; } @property (nonatomic, retain) iboutlet mkmapview *mapview; @property (nonatomic, retain) cllocationmanager *locationmanager; -(void)loadannotations; -(void)showcurrentlocationbuttontapped:(id)sender; @end implementation(partial):
// parkingmapviewcontroller.m - (void)viewdidload { [super viewdidload]; self.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:100 target:self action:@selector(showcurrentlocationbuttontapped:)]; [self loadannotations]; cllocationcoordinate2d centercoord = { ucd_latitude, ucd_longitude }; [mapview setcentercoordinate:centercoord zoomlevel:13 animated:no]; //from "mkmapview+zoomlevel.h" } - (void)showcurrentlocationbuttontapped:(id)sender { nslog(@"showing current location."); //[self.mapview setshowsuserlocation:yes]; //mapview.showsuserlocation = yes; //[sender setenabled:no]; //todo: uncomment , test locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; locationmanager.distancefilter = kcldistancefilternone; // whenever move locationmanager.desiredaccuracy = kcllocationaccuracyhundredmeters; // 100 m [locationmanager startupdatinglocation]; } - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { [mapview setcentercoordinate:newlocation.coordinate]; if ([mapview showsuserlocation] == no) { [mapview setshowsuserlocation:yes];//when line commented, there no problem } [mapview setcentercoordinate:newlocation.coordinate zoomlevel:13 animated:yes]; } - (void)viewdidunload { [super viewdidunload]; [mapview setshowsuserlocation:no]; } - (void)dealloc { [locationmanager release]; [mapview release]; [super dealloc]; } when running application, map view displays fine annotations , everything, when current location button pressed, map re-centers new location(slight move) , fraction of second later, crashes. when comment out [mapview setshowsuserlocation:yes]; there no problem, otherwise spits out error in console:
2010-11-30 22:57:20.657 parking[53430:207] -[mkuserlocation annotationtype]: unrecognized selector sent instance 0x78427f0 2010-11-30 22:57:20.659 parking[53430:207] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[mkuserlocation annotationtype]: unrecognized selector sent instance 0x78427f0' *** call stack @ first throw: ( 0 corefoundation 0x0266ab99 __exceptionpreprocess + 185 1 libobjc.a.dylib 0x027ba40e objc_exception_throw + 47 2 corefoundation 0x0266c6ab -[nsobject(nsobject) doesnotrecognizeselector:] + 187 3 corefoundation 0x025dc2b6 ___forwarding___ + 966 4 corefoundation 0x025dbe72 _cf_forwarding_prep_0 + 50 5 parking 0x00003ddb -[parkingmapviewcontroller mapview:viewforannotation:] + 64 6 mapkit 0x023a8130 -[mkannotationcontainerview _addviewforannotation:] + 175 7 mapkit 0x023a2b2a -[mkannotationcontainerview _addviewsforannotations:animated:] + 251 8 mapkit 0x0239e657 -[mkannotationcontainerview showaddedannotationsanimated:] + 137 9 mapkit 0x0237837c -[mkmapview _showaddedannotationsandrouteanimated:] + 102 10 mapkit 0x02376a88 -[mkmapviewinternal delayedshowaddedannotationsanimated] + 191 11 foundation 0x000571c9 __nsfiretimer + 125 12 corefoundation 0x0264bf73 __cfrunloop_is_calling_out_to_a_timer_callback_function__ + 19 13 corefoundation 0x0264d5b4 __cfrunloopdotimer + 1364 14 corefoundation 0x025a9dd9 __cfrunlooprun + 1817 15 corefoundation 0x025a9350 cfrunlooprunspecific + 208 16 corefoundation 0x025a9271 cfrunloopruninmode + 97 17 graphicsservices 0x02f4900c gseventrunmodal + 217 18 graphicsservices 0x02f490d1 gseventrun + 115 19 uikit 0x002cfaf2 uiapplicationmain + 1160 20 parking 0x000020e0 main + 102 21 parking 0x00002071 start + 53 ) terminate called after throwing instance of 'nsexception' googling has indicated ib problem have not been able spot it. here screen of set in ib:

any whatsoever appreciated. thanks!
the problem looks in viewforannotation method. code trying call annotationtype on mkuserlocation annotation (which doesn't have such method).
in viewforannotation, should check kind of annotation view being requested , handle accordingly.
however, if using mkmapview, don't need use cllocationmanager user's current location. setting showsuserlocation yes make map view show blue dot user located.
in either case, viewforannotation method needs check annotation class type first. should @ top:
- (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation { if ([annotation iskindofclass:mkuserlocation.class]) { //it's built-in user location annotation, return nil default blue dot... return nil; } //handle custom annotations... }
Comments
Post a Comment