MKMapView
An embeddable map interface, similar to the one that the Maps app provides.
Declaration
class MKMapViewOverview
Use this class as-is to display map information and to manipulate the map contents from your app. The map view supports several display styles, including the MKStandardMapConfiguration that provides rich 2D and 3D presentations, an MKHybridMapConfiguration that provides a hybrid satellite map presentation, and MKImageryMapConfiguration that provides an imagery-based map presentation. Each of these map configurations support customization properties that refine specific elements of the map’s presentation.
You can center the map on specific coordinates, specify the size of the area you want to display, and annotate the map with custom information. When you initialize a map view, you specify the initial region for that map to display by setting the region property of the map. MapKit defines a region by a center point and a horizontal and vertical distance, referred to as the span. The span defines how much of the map is visible, and is also how you set the zoom level. For example, specifying a large span results in the user seeing a wide geographical area at a low zoom level, whereas specifying a small span results in a more narrow geographical area and a higher zoom level.
In addition to setting the span programmatically, the MKMapView class supports many standard interactions for changing the position and zoom level of the map. In particular, map views support flick and pinch gestures for scrolling around the map and zooming in and out. The map view enables support for these gestures by default. You can enable and disable them using the isScrollEnabled and isZoomEnabled properties.
You can also use projected map coordinates instead of regions to specify some values. When you project the curved surface of the globe onto a flat surface, you get a two-dimensional version of a map where longitude lines appear to be parallel. To specify locations and distances, you use the MKMapPoint, MKMapSize, and MKMapRect data types.
Don’t subclass the MKMapView class itself. You can get information about the map view’s behavior by providing a delegate object. The map view calls the methods of your custom delegate to let it know about changes in the map status and to coordinate the display of custom annotations. The delegate object can be any object in your app as long as it conforms to the MKMapViewDelegate protocol. For more information about implementing the delegate object, see MKMapViewDelegate.
In macOS 10.14 and later, you can apply a light or dark appearance to your maps by modifying the appearance property of your map view (or one of its ancestor views). Even if you specify a custom appearance, users can use the Maps app to force all maps to adopt a light appearance. Use the map view’s effectiveAppearance property to determine the actual appearance of your map. For information about how to set view appearances, see Choosing a Specific Appearance for Your macOS App.
Annotating the map
The MKMapView class supports the ability to annotate the map with custom information. Because a map may have large numbers of annotations, map views differentiate between the annotation objects MapKit uses to manage the annotation data and the view objects for presenting that data on the map.
An annotation object is any object that conforms to the MKAnnotation protocol. Typically, you implement annotation objects using existing classes in your app’s data model. This allows you to manipulate the annotation data directly, but still make it available to the map view. Each annotation object contains information about the annotation’s location on the map, along with descriptive information that the map can display in a callout.
An annotation view,_ _which is an instance of the MKAnnotationView class, handles the presentation of annotation objects on the screen. An annotation view is responsible for presenting the annotation data in a way that makes sense. For example, the Maps app uses a marker icon to denote specific points of interest on a map. The MapKit framework offers the MKMarkerAnnotationView class for similar annotations in your own apps. You can also create annotation views that cover larger portions of the map.
Because the map view needs annotation views only when they’re onscreen, the MKMapView class provides a mechanism for queueing annotation views that aren’t in use. The map view detaches annotation views with a reuse identifier and queues them internally when they move offscreen. This feature improves memory use by keeping only a small number of annotation views in memory at once, and by recycling the views you do have. It also improves scrolling performance by alleviating the need to create new views while the map is scrolling.
When configuring your map interface, be sure to add all of your annotation objects right away. The map view uses the coordinate data in each annotation object to determine when the corresponding annotation view needs to appear onscreen. When an annotation moves onscreen, the map view asks its delegate to create a corresponding annotation view. If your app has different types of annotations, it can define different annotation view classes to represent each type.
Adding overlays to the map
You can use overlays to layer content over a wide portion of the map. An overlay object is any object that conforms to the MKOverlay protocol. An overlay object is a data object that contains the points that specify the shape and size of the overlay and its location on the map. Overlays can represent shapes like circles, rectangles, multisegment lines, and simple or complex polygons. You can also define your own custom overlays to represent other shapes.
Overlay renderer objects, which are instances of the MKOverlayRenderer class, handle the presentation of an overlay. The job of the renderer is to draw the overlay’s content onto the screen when the map view requests it. For example, if you have a simple overlay that represents a bus route, you can use a polyline renderer to draw the line segments that trace the route of the bus. You can also define a custom renderer that draws both the bus route and icons at the location of each bus stop. When specifying overlays, you can add them to specific levels of the map, which tells the map view to render them above or below other types of map content.
When configuring your map interface, you can add overlay objects at any time. The map view uses the data in each overlay object to determine when the corresponding overlay view needs to appear onscreen. When an overlay moves onscreen, the map view asks its delegate to create a corresponding overlay renderer.
Adding points of interest to the map
In iOS16 and macOS 13, and later, you can configure the map view to allow people to interact with a wide variety of points of interest (POIs) the map displays. These are instances of the MKMapFeatureAnnotation class, and cover a wide variety of elements visible on the map, including:
Points of interest, such as museums, cafes, parks, and schools.
Territorial boundaries, such as national borders, state boundaries, and neighborhoods.
Features on the Earth’s surface, such as mountain ranges, rivers, and ocean basins.
You can control which features a person can interact with by configuring one of the MKMapConfiguration subclasses that defines the map’s presentation. Create an MKMapConfiguration with a set of MKMapFeatureOptions that describe the categories of POIs the map responds to. To further refine the specific kinds of points of interest the map display presents, use an MKPointOfInterestFilter.
When a person interacts with a specific POI, the framework calls your delegate object with one of the MKMapViewDelegate protocol methods, depending on whether the person selects or deselects a specific POI. These methods give your app a chance to respond to the selection or deselection of an element. Depending on the kind of element, you can decide whether you want to customize the display characteristics in the case of a POI, or in the case of territories or geographic map features, you can create custom interactions to display information.
Adding Look Around views to the map
iOS16 and macOS 13, and later, support the inclusion of a Look Around view within the map view. Look Around allows people to explore the environment at street level. You request a Look Around view by creating an MKLookAroundSceneRequest with either an MKMapItem or a CLLocationCoordinate2D, and if there’s Look Around imagery available for the specified location, the framework returns an MKLookAroundScene for you to display using an MKLookAroundViewController.
Topics
Configuring the map appearance
preferredConfigurationpitchButtonVisibilityshowsUserTrackingButtonMKMapConfigurationMKStandardMapConfigurationMKHybridMapConfigurationMKImageryMapConfiguration
Customizing the map view behavior
Accessing map properties
Manipulating the visible portion of the map
regionsetRegion(_:animated:)centerCoordinatesetCenter(_:animated:)showAnnotations(_:animated:)visibleMapRectsetVisibleMapRect(_:animated:)setVisibleMapRect(_:edgePadding:animated:)
Constraining the map view
setCameraBoundary(_:animated:)cameraBoundarysetCameraZoomRange(_:animated:)cameraZoomRangeMKMapView.CameraBoundaryMKMapView.CameraZoomRange
Configuring the map display
setCamera(_:animated:)camerashowsCompassshowsPitchControlshowsScaleshowsZoomControlsshowsBuildingsshowsPointsOfInterestpointOfInterestFiltershowsTraffic
Displaying the user’s location
Converting a user’s location to a descriptive placemarkshowsUserLocationisUserLocationVisibleuserLocationuserTrackingModesetUserTrackingMode(_:animated:)MKUserTrackingMode
Annotating the map
annotationsaddAnnotation(_:)addAnnotations(_:)removeAnnotation(_:)removeAnnotations(_:)annotations(in:)
Managing annotation selections
annotationVisibleRectselectedAnnotationsselectAnnotation(_:animated:)deselectAnnotation(_:animated:)
Creating annotation views
register(_:forAnnotationViewWithReuseIdentifier:)dequeueReusableAnnotationView(withIdentifier:for:)dequeueReusableAnnotationView(withIdentifier:)view(for:)MKMapViewDefaultAnnotationViewReuseIdentifierMKMapViewDefaultClusterAnnotationViewReuseIdentifier
Accessing overlays
Adding and inserting overlays
addOverlay(_:level:)addOverlays(_:level:)addOverlay(_:)addOverlays(_:)insertOverlay(_:at:level:)insertOverlay(_:at:)insertOverlay(_:above:)insertOverlay(_:below:)exchangeOverlay(_:with:)exchangeOverlay(at:withOverlayAt:)