The MapView is the object that can display an embeddable map interface in the iOS applications. It is similar to the one that is provided by the Maps applications. It is an instance of the MKMapView class, which inherits the UIView class.
To display the map view in the iOS application, we need to search for the MKMapView in the object library and drag the result to the storyboard, as shown in the following image.
However, if we add the map view to the interface, define the auto-layout rules for the map view and run the application, the simulator will display the output shown in the following image.
To use the MapView in the iOS applications, we must import the MapKit since MKMapView exists in iOS MapKit. MapKit is a powerful API for the iOS devices using which we can display the locations, show routes, shapes, and a lot more geographical content. By using the properties and methods present in the MapKit, we can zoom in to a particular location, center the map on the specified coordinates, specify the size of the area we want to display and annotate the map with the custom location. We can initialize the map view with the region property to specify a particular region of the map.
A region is the center point defined by horizontal and vertical distance referred to as span. A span defines the visibility of the map. For example, specifying the large span results into the wide geographical area at the low zoom level.
MapView Example 1: Setting the initial region in the map
In this example, we will set the initial region of the map view. For this purpose, we need to create a location object by instantiating the CLLocation class. The location object can be created using the following syntax.
This method accepts two arguments that are the latitude and longitude of the particular location. In MapKit, we latitude and longitude are the instance of CLLocationDegrees class.
Showing ArtWork on the Map
MapKit facilitates us to show the artwork for the center location of the map. To show this on the map view, we must create a map annotation. Map annotations are defined as the small pieces of information that are shown for a particular location. The annotations on Apple?s map are represented by small pins.
In this example, we will create the annotation for the center location of the map. To create the annotation, we must create a class that conforms to the MKAnnotation protocol. The ArtWork.swift class conforms to the MKAnnotation protocol, which is then added to Map in ViewController.swift class.
The properties defined in the MKMapView class are given in the following table.
||var delegate: MKMapViewDelegate?
||It is the receiver's delegate of type MKMapViewDelegate Protocol.
||var mapType: MKMapType
||It is the type of data displayed by the map view.
||var isZoomEnabled: Bool
||It is a boolean type value that indicates whether the zoom is enabled for the map view or not.
||var isScrollEnabled: Bool
||It is a boolean type value that indicates whether the scroll is enabled for the map view or not.
||var isPitchEnabled: Bool
||It is a boolean type value that indicates whether the pitch information of the map camera is used or not.
||var isRotateEnabled: Bool
||It is a boolean type value that indicates whether the map camera's heading information is used or not.
||var region: MKCoordinateRegion
||It is the initial region displayed by the map.
||var centerCoordinate: CLLocationCoordinate2D
||The coordinate to be displayed by the map center.
||var visibleMapRect: MKMapRect
||It is the area currently displayed by the map view.
||var cameraBoundary: MKMapView.CameraBoundary?
||It represents the boundary of the area within which the center of the mapview must remain.
||var cameraZoomRange: MKMapView.CameraZoomRange!
||It represents the zoom range of the map camera.
||var camera: MKMapCamera
||It represents the camera used for determining the appearance of the map.
||var pointOfInterestFilter: MKPointOfInterestFilter?
||It represents the filter used to determine the point of the interest shown on the map.
||var showsBuildings: Bool
||It is the boolean value representing whether to display the building's information.
||var showsCompass: Bool
||It is the boolean value indicating whether the map displays the compass control.
||var showsZoomControls: Bool
||It is a boolean value indicating whether the map displays the zoom control.
||var showsScale: Bool
||It is a boolean value indicating whether the map displays the scale information.
||var showsTraffic: Bool
||It is a boolean value that indicates whether to display the traffic information.
||var showsUserLocation: Bool
||It is a boolean value that indicates whether the map should try to display the user?s location.
||var isUserLocationVisible: Bool
||It is a boolean value that indicates whether the current location of the user is displayed on the map view.
||var userLocation: MKUserLocation
||It represents the user's current location.
||var userTrackingMode: MKUserTrackingMode
||It represents the mode used to track the user's current location.
||var annotations: [MKAnnotation]
||It is the array of the annotations for the different map center locations.
||var annotationVisibleRect: CGRect
||It represents the visible rectangle where annotation views are currently being displayed.
||var selectedAnnotations: [MKAnnotation]
||It represents the annotations that are currently being selected.
||var overlays: [MKOverlay]
||It represents the overlay objects currently associated with the map view.