ios - Show User's current location on map -
i trying display user's current location on map reason, not takes default coordinates.
i have mkmapview class called inside uiviewcontroller.
mapview.h
#import <uikit/uikit.h> #import <mapkit/mapkit.h> #import <corelocation/corelocation.h> @interface mapview : mkmapview <mkmapviewdelegate, cllocationmanagerdelegate>{ cllocationmanager *locationmanager; } @end
mapview.m
#import "mapview.h" @interface mapview () @end @implementation mapview - (id)initwithframe:(cgrect)frame{ self = [super initwithframe:frame]; if (self) { locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; self.delegate = self; [self addannotation:[self userlocation]]; [locationmanager startupdatinglocation]; } return self; } - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { nslog(@"didfailwitherror: %@", error); uialertview *erroralert = [[uialertview alloc] initwithtitle:@"error" message:@"failed location" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [erroralert show]; } - (void)mapview:(mkmapview *)mapview didupdateuserlocation:(mkuserlocation *)userlocation { nslog(@"didupdateuserlocation"); mkcoordinateregion region = mkcoordinateregionmakewithdistance(userlocation.coordinate, 250, 250); [self setregion:[self regionthatfits:region] animated:yes]; mkpointannotation *point = [[mkpointannotation alloc] init]; point.coordinate = userlocation.coordinate; point.title = @"where i?"; point.subtitle = @"i'm here!!!"; [self addannotation:point]; } - (mkannotationview*)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation{ nslog(@"viewforannotation"); static nsstring *identifier = @"myannotation"; if ([annotation iskindofclass:[mkuserlocation class]]) { nslog(@"is user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude); return nil; } return nil; } - (void)mapview:(mkmapview *)mapview didaddannotationviews:(nsarray *)views{ nslog(@"didaddannotationviews"); (mkannotationview *view in views){ if ([[view annotation] iskindofclass:[mkuserlocation class]]){ mkcoordinateregion region = mkcoordinateregionmakewithdistance([[view annotation] coordinate] , 250, 250); [mapview setregion:region animated:yes]; } } } @end
i getting output below once access map.
viewforannotation user 0.000000, 0.000000 didaddannotationviews
i can't understand why didupdateuserlocation:
never called if re-submit custom coordinates.
what missing here?
you're using cllocationmanager
, setting it's delegate
, delegate methods methods of mkmapviewdelegate
.
you can either set mapview's delegate, or use locationmanager:didupdatelocations:
cllocationmanagerdelegate
method.
Comments
Post a Comment