i need on .... drop pins.
current location.... pin drop.... blue.... event location :locations latitude:53.373812...longitude 4.890951 red pin.
i did this:
@interface addressannotation : nsobject<mkannotation> { cllocationcoordinate2d coordinate; nsstring *mtitle; nsstring *msubtitle; // cllocationmanager *locationmanager; // cllocation *currentlocation; } @end @interface mapviewcontroller : uiviewcontroller <cllocationmanagerdelegate> { 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
//implementation file.
#import "mapviewcontroller.h" @implementation addressannotation @synthesize coordinate; //@synthesize currentlocation; - (nsstring *)subtitle{ //return @"sub title"; return @"event"; } - (nsstring *)title{ //return @"title"; return @"location "; } -(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"); [locationmanager startupdatinglocation]; } -(void)stop{ [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.2; span.longitudedelta=0.2; 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]]; 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]; } else { //show error } cllocationcoordinate2d location; location.latitude = latitude; location.longitude = longitude; return location; } - (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation{ 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 { [super viewdidunload]; // release retained subviews of main view. // e.g. self.myoutlet = nil; } - (void)dealloc { [address release]; [super dealloc]; } @end
please me out...
thanks in adavance.
showing user current location simple.
-(void)start{ nslog(@"start"); mapview.showsuserlocation=yes; //this show current location blue dot in mapview [locationmanager startupdatinglocation]; } -(void)stop{ mapview.showsuserlocation=no; [locationmanager stopupdatinglocation]; }
in viewforannotation delegate
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { if (annotation == mapview.userlocation) { // code execute when current location called. 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; }
Comments
Post a Comment