MKMapView (iOS – Xcode)MapKit is an iOS framework used to display maps, show locations, and add annotations (pins) inside an app.
👉 It allows apps to:
MKMapView is the main UI component of MapKit that displays the actual interactive map.
👉 Simple idea:
Used to get the device’s:
Used together with MapKit:
CLLocationManager → tracks user locationA pin/marker on map showing a location.
Defines:
import MapKit
import CoreLocation
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
let coordinate = CLLocationCoordinate2D(latitude: 31.5204, longitude: 74.3587)
let region = MKCoordinateRegion(center: coordinate,
latitudinalMeters: 1000,
longitudinalMeters: 1000)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.title = "Lahore"
annotation.subtitle = "Pakistan"
annotation.coordinate = CLLocationCoordinate2D(latitude: 31.5204,
longitude: 74.3587)
mapView.addAnnotation(annotation)
User Location → CLLocationManager
↓
Coordinates (Lat, Long)
↓
MKMapView
↓
Display Map + Pin (Annotation)
class ViewController: UIViewController, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
pin.pinTintColor = .red
pin.canShowCallout = true
return pin
}
}
showsUserLocation carefullyPrivacy - Location When In Use Usage Description
We need your location to show nearby places.
MapKit = framework for maps
MKMapView = displays map UI
CLLocationManager = gets user location
Features:
Needs permission from user
Used in:
Open this section to load past papers