0
0
iOS Swiftmobile~5 mins

MapKit for maps in iOS Swift

Choose your learning style9 modes available
Introduction

MapKit helps you show maps in your app so users can see locations and directions easily.

Showing a map to help users find a store or restaurant nearby.
Displaying a user's current location on a map.
Adding pins to mark important places on a map.
Providing directions from one place to another inside your app.
Syntax
iOS Swift
import MapKit

let mapView = MKMapView()

// Set map region
let coordinate = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: coordinate, span: span)
mapView.setRegion(region, animated: true)

Import MapKit to use map features.

MKMapView is the view that shows the map.

Examples
Create a map view to add to your screen.
iOS Swift
import MapKit

let mapView = MKMapView()
Set the map to show New York City with a zoom level.
iOS Swift
let coordinate = CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: coordinate, span: span)
mapView.setRegion(region, animated: true)
Add a pin on the map with a title.
iOS Swift
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060)
annotation.title = "New York"
mapView.addAnnotation(annotation)
Sample App

This SwiftUI view shows a full-screen map centered on San Francisco.

iOS Swift
import SwiftUI
import MapKit

struct ContentView: View {
    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
        span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
    )

    var body: some View {
        Map(coordinateRegion: $region)
            .edgesIgnoringSafeArea(.all)
    }
}
OutputSuccess
Important Notes

You can customize the map style and add pins or overlays.

Remember to add the "Privacy - Location When In Use Usage Description" key in your app's Info.plist if you use user location.

Use the MapKit framework only on real devices or simulators with location support.

Summary

MapKit lets you add maps to your iOS app easily.

You create an MKMapView or use SwiftUI's Map view to show maps.

You can set the map region and add pins to mark places.