Map​Kit:Show current location on map of your custom apps using SWIFT.

MapKit
Introduction to Mapkit:
The MapKit Framework is based on the Apple Maps data and APIs.The heart of the MapKit framework from the point of view of the developer is MKMapView.
You use this class  to display map information and to manipulate the map contents from your application. You can center the map on a given coordinate, annotate the map with custom information and specify the size of the area you want to display.

Implementation of the MKMapViewDelegate protocol allows an application to receive notifications of events relating to the map view such as a change in either the location of the user or region of the map displayed or the failure of the device to identify the user’s current location or to download map data.

You can display standard street-level map information, satellite imagery, or a combination of the two. You can zoom, pan, and pitch the map programmatically, display 3D buildings, and annotate the map with custom information.

How you specify data points on a map depends on how you intend to use them. Map Kit supports three basic coordinate systems for specifying map data points:
  • A map coordinate is a latitude and longitude on the spherical representation of the Earth. Map coordinates are the primary way of specifying locations on the globe. You specify individual map coordinate values using the CLLocationCoordinate2D structure. You can specify areas using the MKCoordinateSpan andMKCoordinateRegion structures.
  • A map point is an x and y value on the Mercator map projection. Map points are used for many map-related calculations instead of map coordinates because they simplify the mathematics involved in the calculations. In your app, you use map points primarily when specifying the shape and position of custom map overlays. You specify individual map points using the MKMapPoint structure. You can specify areas using the MKMapSize and MKMapRect structures.
  • A point is a graphical unit associated with the coordinate system of a view object. Map points and map coordinates must be mapped to points before drawing custom content in a view. You specify individual points using the CGPoint structure. You can specify areas using the CGSize and CGRect structures.
Adding a Map View to Your User Interface like below


Take a outlet of a MapView

//Add following Code into Info.plist

<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>

//Displaying the User’s Current Location on the Map


import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController{
   @IBOutlet var mapView : MKMapView!

   var CLManager = CLLocationManager()
   override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view, typically from a nib
       CLManager.requestWhenInUseAuthorization()
        mapView.showsUserLocation = true
   }

//To set the MapView Region to userLocation

Write following code into viewDidLoad()
       CLManager.delegate = self
      CLManager.desiredAccuracy = kCLLocationAccuracyBest
      CLManager.requestWhenInUseAuthorization()
      CLManager.startUpdatingLocation()

   //Write  CLLocationManagerDelegate Methods  below the viewDidLoad()   

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       
       let Userlocation : CLLocation = locations[0]
       
       let center = CLLocationCoordinate2D(latitude: Userlocation.coordinate.latitude
           , longitude: Userlocation.coordinate.longitude)
       
       let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
     
      mapView.setRegion(region, animated: true)
       
   }
   
   func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
   {
       print(error)   
   }

//To Change Map Type

The map type of a map view is controlled by the object’s mapType property. Supported values for this property are MKMapType.standard, MKMapType.satellite MKMapType.hybrid, MKMapType.satelliteFlyover and MKMapType.hybridFlyover.

Just write following  code to  which type of map you required  into viewDidLoad()

       mapView.mapType = .standard
      //mapView.mapType = .hybrid
      //mapView.mapType = .hybridFlyover
     // mapView.mapType = .satellite
      //mapView.mapType = .satelliteFlyover
    

adding  pin to MKMapView

let annotation = MKPointAnnotation()
annotation.coordinate = center
annotation.title = "title"
Annotation.subtitle = “Subtitle”
mapView.addAnnotation(annotation)


Connecting me socially=  Twitter  Linkedin Facebook 

Comments