This one was great. I tried to figure this out on my own before and did not find NSdateFormatter.
There is a little extra code in there as I was trying to figure out how to access the coordinate latitude and longitude. Ignore that.
CLLocationCoordinate2D coord = [loc coordinate];
/* __________________________________________________________ If you want to print out the current latitude and longitude. This is how you access members of the "coord" structure, which hold double (float) values. Therefore use %f, and it works. NSLog(@"The current latitude is %f.", coord.latitude); NSLog(@"The current longitude is %f.", coord.longitude); ___________________________________________________________ */ //Create an instance of BNRMapPoint with the current data //Get the current date and time. NSDate *today = [NSDate date]; //Set up a date formatter. NSDateFormatter *dFormatter = [[NSDateFormatter alloc] init]; [dFormatter setTimeStyle:NSDateFormatterNoStyle]; [dFormatter setDateStyle:NSDateFormatterMediumStyle]; //Get the current date string using the formatter. NSString *dateString = [@": " stringByAppendingString:[dFormatter stringFromDate:today]]; //To the title and the colon, add the date string. NSString *outputString = [[locationTitleField text] stringByAppendingString:dateString]; BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:outputString]; //Add it to the map view [worldView addAnnotation:mp];[/code]
I wanted to go a step further and also put in the latitude and longitude on the map point title, but it’s too long. I’m not sure whether such a title can have multiple lines.
It works, but I doubt that it’s the way it should be done. Seems like a lot of lines of code to get a string!