how current location green pin , destination location red pin?
when work on stuff destination location red pin, not @ current location.
my source code.
#import <uikit/uikit.h> #import <mapkit/mapkit.h> #import <mapkit/mkannotation.h> #import <corelocation/corelocation.h> @interface addressannotation : nsobject<mkannotation> { cllocationcoordinate2d coordinate; nsstring *mtitle; nsstring *msubtitle; // cllocationmanager *locationmanager; // cllocation *currentlocation; } @end @interface mapviewcontroller : uiviewcontroller <cllocationmanagerdelegate,mkmapviewdelegate> { iboutlet mkmapview *mapview; addressannotation *addannotation; nsstring *address; cllocationmanager *locationmanager; cllocation *currentlocation; } +(mapviewcontroller *)sharedinstance; -(void)start; -(void)stop; -(bool)locationknown; @property(nonatomic,retain)cllocation *currentlocation; @property(nonatomic,retain)nsstring *address; -(cllocationcoordinate2d) addresslocation; -(void)showaddress; @end #import "mapviewcontroller.h" @implementation addressannotation @synthesize coordinate; //@synthesize currentlocation; - (nsstring *)subtitle{ //return @"sub title"; return @"event"; } - (nsstring *)title{ //return @"title"; return @"allure-exclusive"; } -(id)initwithcoordinate:(cllocationcoordinate2d) c{ coordinate=c; //nslog(@"%f,%f",c.latitude,c.longitude); return self; } @end @implementation mapviewcontroller @synthesize address; @synthesize currentlocation; static mapviewcontroller *sharedinstance; +(mapviewcontroller *)sharedinstance{ @synchronized (self) { if (!sharedinstance) [[mapviewcontroller alloc]init]; } return sharedinstance; } +(id)alloc{ @synchronized(self){ nsassert(sharedinstance==nil,"attempted allocate second instance of singleton locationcontroller."); sharedinstance = [super alloc]; } return sharedinstance; } -(id)init{ if(self==[super init]){ self.currentlocation=[[cllocation alloc]init]; locationmanager=[[cllocationmanager alloc]init]; locationmanager.delegate=self; [self start]; } return self; } -(void)start{ nslog(@"start"); mapview.showsuserlocation=yes; [locationmanager startupdatinglocation]; } -(void)stop{ mapview.showsuserlocation=no; [locationmanager stopupdatinglocation]; } -(bool)locationknown{ if (round(currentlocation.speed)==-1) return no; else return yes; } - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { if (abs([newlocation.timestamp timeintervalsincedate:[nsdate date]])<120){ self.currentlocation=newlocation; } } - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { uialertview *alert; alert=[[uialertview alloc]initwithtitle:@"error" message:[error description] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; [alert release]; } // implement viewdidload additional setup after loading view, typically nib. - (void)viewdidload { [super viewdidload]; self.title=@"map-view"; // [self addresslocation]; [self showaddress]; nslog(@"address %@",address); } -(void)showaddress{ mkcoordinateregion region; mkcoordinatespan span; span.latitudedelta=0.5; span.longitudedelta=0.5; cllocationcoordinate2d location = [self addresslocation]; region.span=span; region.center=location; if(addannotation != nil) { [mapview removeannotation:addannotation]; [addannotation release]; addannotation = nil; } addannotation = [[addressannotation alloc] initwithcoordinate:location]; [mapview addannotation:addannotation]; [mapview setregion:region animated:true]; [mapview regionthatfits:region]; } -(cllocationcoordinate2d) addresslocation { nsstring *urlstring = [nsstring stringwithformat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; nsstring *locationstring = [nsstring stringwithcontentsofurl:[nsurl urlwithstring:urlstring]]; nslog(@"locationstring %@",locationstring); nsarray *listitems = [locationstring componentsseparatedbystring:@","]; double latitude = 0.0; double longitude = 0.0; if([listitems count] >= 4 && [[listitems objectatindex:0] isequaltostring:@"200"]) { latitude = [[listitems objectatindex:2] doublevalue]; longitude = [[listitems objectatindex:3] doublevalue]; nslog(@"listitems %@",[listitems objectatindex:2]); } else { //show error } cllocationcoordinate2d location; location.latitude = latitude; location.longitude = longitude; return location; } - (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation{ if (annotation==mapview.userlocation) { mkpinannotationview *annview=[[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"currentloc"]; annview.pincolor = mkpinannotationcolorgreen; annview.animatesdrop=yes; annview.canshowcallout = yes; annview.calloutoffset = cgpointmake(-5, 5); return annview; // } else { mkpinannotationview *annview=[[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"currentloc"]; annview.pincolor = mkpinannotationcolorred; annview.animatesdrop=yes; annview.canshowcallout = yes; annview.calloutoffset = cgpointmake(-5, 5); return annview; } } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // overriden allow orientation. return yes; } - (void)didreceivememorywarning { // releases view if doesn't have superview. [super didreceivememorywarning]; // release cached data, images, etc aren't in use. } - (void)viewdidunload { // [self stop]; [super viewdidunload]; // release retained subviews of main view. // e.g. self.myoutlet = nil; } - (void)dealloc { [address release]; [super dealloc]; } @end - (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation{ if (annotation==mapview.userlocation) { mapview.userlocation.title=@"current location"; [mapview setregion:mkcoordinateregionmakewithdistance(mapview.userlocation.coordinate, 1000, 1000)animated:yes]; return nil; } else { mkpinannotationview *annview=[[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"currentloc"]; annview.pincolor = mkpinannotationcolorred; annview.animatesdrop=yes; annview.canshowcallout = yes; annview.calloutoffset = cgpointmake(-5, 5); return annview; } }
when changed method. it's pointing blue , blinking,but pointing @ different location, @ infinite loop mariani ave location.
it running in simulator.
you'll want set showsuserlocation.
mapview.showsuserlocation = yes;
Comments
Post a Comment