
MapKit is an iOS framework used to display maps, show locations, and add annotations (pins) inside an app.
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 view and download past papers
Click on Show Past Papers to load available exam papers.