Tuesday, 27 March 2018

Apple Map -- Swift4

Plist:

Privacy - Location When In Use Usage Description
Privacy - Location Usage Description

Code Implementation:


import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController , CLLocationManagerDelegate ,MKMapViewDelegate {
    @IBOutlet weak var loca_map: MKMapView!
    let locationMgr = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        loca_map.delegate=self
        loca_map.showsUserLocation=true
        locationMgr.delegate = self
        
        locationMgr.delegate = self;
        locationMgr.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
       
        let authorizationStatus = CLLocationManager.authorizationStatus()
        
        if (authorizationStatus == CLAuthorizationStatus.notDetermined) || (authorizationStatus == nil) {
            locationMgr.requestWhenInUseAuthorization()
        } else {
            locationMgr.startUpdatingLocation()
        }
        
        let coord = CLLocation (latitude: 12.961945, longitude: 80.258784)
        
        let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
                                                  longitude: coord.coordinate.longitude);
        let anno = MKPointAnnotation();
        anno.coordinate = CLLCoordType;
        loca_map.addAnnotation(anno);
        
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.last!
        print("Current location: \(currentLocation)")
        let viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 500, 500)
        loca_map.setRegion(viewRegion, animated: false)
    }

}

No comments:

Post a Comment