Wednesday, 20 December 2017

Mapview Demo

step 1: Drag and drop MKMapView on ViewController.

step 2: Give constraints on MKMapView.

step 3: Add following Frameworks.
1) CoreLocation.Framework
2) MapKit.Framework

step 4: In Info.plist give privacy.
1) Privacy - Location Always Usage Description
2) Privacy - Location When In Use Usage Description

step 5: Create Location manager and Annotation. Like,

@IBOutlet var mapViww: MKMapView!
    var locationManager : CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        //This is for create Location Manager...
        locationManager = CLLocationManager()
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        
        //This is for create annotation...
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(23.3072, 73.1812)
        annotation.title = "baroda"
        annotation.subtitle = "far"
        mapViww.addAnnotation(annotation)
       
        let annotationAhmdbd = MKPointAnnotation()
        annotationAhmdbd.coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714)
        annotationAhmdbd.title = "Ahmedabad"
        annotationAhmdbd.subtitle = "near"
        mapViww.addAnnotation(annotationAhmdbd)
        // Do any additional setup after loading the view, typically from a nib.

    }

step 6: To create Update Location use following code. Like,

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        print("didUpdateLocations latitude:  \(locations.first!.coordinate.latitude) ,  longitude:  \(locations.first!.coordinate.longitude)")
        let location = CLLocationCoordinate2DMake(locations.first!.coordinate.latitude, locations.first!.coordinate.longitude)
        
        let span = MKCoordinateSpanMake(0.01, 0.01)
        let region = MKCoordinateRegionMake(location, span)
        mapViww.setRegion(region, animated: true)
    }

Thank you...


No comments:

Post a Comment