iOS Content Views

iOS: Map View

The component that allows an embeddable map interface to be displayed in iOS applications is called MapView. That is comparable to the one offered by the Maps apps. It is an example of the MKMapView class, which has the UIView class as a parent class.

				
					class MKMapView : UIView  
				
			

As seen in the following image, we must look for the MKMapView in the object library and drag the result to the storyboard in order to display the map view in the iOS application.

iOS: Map View -

On the other hand, the simulator will display the output seen in the following image if we add the map view to the interface, provide the auto-layout rules for the map view, and execute the application.

iOS: Map View -

Since MKMapView is already included in iOS MapKit, we must import MapKit in order to use MapView in iOS applications. With the help of the robust MapKit API for iOS devices, we can display locations, routes, shapes, and a wealth of other geographic information. We may zoom in to a specific location, center the map on given coordinates, define the size of the area we want to display, and annotate the map with a custom location by utilizing the properties and methods available in the MapKit. To target a specific area on the map, we can initialize the map view with the region attribute.

A region’s center point, known as its span, is determined by its horizontal and vertical distances. The map’s visibility is defined by a span. For instance, at low zoom levels, choosing a long span yields a huge geographic area.

MapView Example 1: Setting the initial region in the map

We’ll set the map view’s initial region in this example. We must instantiate the CLLocation class in order to build a location object for this purpose. The following syntax can be used to build the location object.

				
					let location = CLLocation(latitude: CLLocationDegrees,  longitude: CLLocationDegrees#)  
				
			

The latitude and longitude of the specific location are the two inputs that this method accepts. Latitude and longitude in Map Kit are instances of the CL Location Degrees class.

ViewController.swift

				
					import UIKit  
import MapKit  
  
  
class ViewController: UIViewController {  
  
  
    @IBOutlet weak var mapView: MKMapView!  
      
    let location = CLLocation(latitude: 28.613939, longitude: 77.209023)  
      
    let region_radious = 10000  
      
    override func viewDidLoad() {  
        super.viewDidLoad()  
        // Do any additional setup after loading the view.  
        centreMap(location: location)  
          
    }  
      
    func centreMap(location: CLLocation){  
        let region = MKCoordinateRegion(center: location.coordinate,latitudinalMeters: CLLocationDistance(region_radious), longitudinalMeters: CLLocationDistance(region_radious))  
        mapView.setRegion(region, animated: true)  
          
    }  
}  
				
			

Output

iOS: Map View -

Showing ArtWork on the Map

We are able to display the artwork at the map’s center thanks to MapKit. We need to make a map annotation in order to display this on the map view. The brief details that are displayed for a specific location on a map are known as annotations. Tiny pins are used on Apple’s map to symbolize annotations.

We will generate the annotation for the map’s center in this example. We need to make a class that complies with the MKAnnotation protocol before we can construct the annotation. The MKAnnotation protocol is followed by the ArtWork.swift class and added to the Map in the ViewController.swift code.

ArtWork.swift

				
					import Foundation  
import MapKit  
  
  
class ArtWork : NSObject, MKAnnotation {  
    var coordinate: CLLocationCoordinate2D  
      
      
    var title : String?  
    var subtitle: String?  
      
    public init (coordinate: CLLocationCoordinate2D, title: String, subtitle: String){  
        self.coordinate = coordinate  
        self.title = title  
        self.subtitle = subtitle  
    }  
} 
				
			

ViewController.swift

				
					import Foundation  
import MapKit  
  
  
class ArtWork : NSObject, MKAnnotation {  
    var coordinate: CLLocationCoordinate2D  
      
      
    var title : String?  
    var subtitle: String?  
      
    public init (coordinate: CLLocationCoordinate2D, title: String, subtitle: String){  
        self.coordinate = coordinate  
        self.title = title  
        self.subtitle = subtitle  
    }  
}  
				
			

Output

iOS: Map View -

MKMapView Properties

The following table lists the attributes that are declared in the MKMapView class.

SNPropertyDescription
1var delegate: MKMapViewDelegate?It is the receiver’s delegate of type MKMapViewDelegate Protocol.
2var mapType: MKMapTypeIt is the type of data displayed by the map view.
3var isZoomEnabled: BoolIt is a boolean type value that indicates whether the zoom is enabled for the map view or not.
4var isScrollEnabled: BoolIt is a boolean type value that indicates whether the scroll is enabled for the map view or not.
5var isPitchEnabled: BoolIt is a boolean type value that indicates whether the pitch information of the map camera is used or not.
6var isRotateEnabled: BoolIt is a boolean type value that indicates whether the map camera’s heading information is used or not.
7var region: MKCoordinateRegionIt is the initial region displayed by the map.
8var centerCoordinate: CLLocationCoordinate2DThe coordinate to be displayed by the map center.
9var visibleMapRect: MKMapRectIt is the area currently displayed by the map view.
10var cameraBoundary: MKMapView.CameraBoundary?It represents the boundary of the area within which the center of the mapview must remain.
11var cameraZoomRange: MKMapView.CameraZoomRange!It represents the zoom range of the map camera.
12var camera: MKMapCameraIt represents the camera used for determining the appearance of the map.
13var pointOfInterestFilter: MKPointOfInterestFilter?It represents the filter used to determine the point of the interest shown on the map.
14var showsBuildings: BoolIt is the boolean value representing whether to display the building’s information.
15var showsCompass: BoolIt is the boolean value indicating whether the map displays the compass control.
16var showsZoomControls: BoolIt is a boolean value indicating whether the map displays the zoom control.
17var showsScale: BoolIt is a boolean value indicating whether the map displays the scale information.
18var showsTraffic: BoolIt is a boolean value that indicates whether to display the traffic information.
19var showsUserLocation: BoolIt is a boolean value that indicates whether the map should try to display the user?s location.
20var isUserLocationVisible: BoolIt is a boolean value that indicates whether the current location of the user is displayed on the map view.
21var userLocation: MKUserLocationIt represents the user’s current location.
22var userTrackingMode: MKUserTrackingModeIt represents the mode used to track the user’s current location.
23var annotations: [MKAnnotation]It is the array of the annotations for the different map center locations.
24var annotationVisibleRect: CGRectIt represents the visible rectangle where annotation views are currently being displayed.
25var selectedAnnotations: [MKAnnotation]It represents the annotations that are currently being selected.
28var overlays: [MKOverlay]It represents the overlay objects currently associated with the map view.
Share this Doc

iOS: Map View

Or copy link

Explore Topic