Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Junaid Abdurahman
Courses Plus Student 108 PointsHow do add a map with points on it on it? (Xcode)
How do I add a map to my app of a city and then have pins on it where if the user touches it a button will appear (Like the standard iOS Maps App) and when the user touches the button it takes them to a view controller. Language Swift
1 Answer
landonferrier
25,097 PointsJunaid, creating a pin and adding it into a MKMapView is very easy with the MKPointAnnotation class.
I am unaware of your map view setup, but this code will need to be added after you create a map view (if you create the map in a method or block) or in viewDidLoad: (if the map is a property of your class). I create this pin with a coordinate (required) that I found on Google Maps for a warm place (Bahamas). You will want to set the coordinate property to something the map is used for (business location, favorite restaurant, etc.). Also wanted to point out the subtitle property is not required and I only added it to illustrate its existence.
//Create the pin with the default alloc and init methods.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//Set the cordinate with the handy CLLocationCoordinate2DMake: method.
annotation.coordinate = CLLocationCoordinate2DMake(24.707094, -77.773972);
//Set the title of the pin, this will show above the subtitle in a larger font.
annotation.title = @"Paradise";
//Set the subtitle of the pin, this will show under the title in a smaller font.
annotation.subtitle = @"A cool subtitle";
//Now that the pin has been created and configured, add it to the map view so it is visible.
[self.mapView addAnnotation:annotation];
(If you do not know how to find the longitude and latitude, just post a comment and I would love to help!)
If you have any questions, feel free to ask!