Swift 3 / 4 get current location with location manager .

Home  >>  Tutorials  >>  IOS  >>  Swift 3 / 4 get current location with location manager .

Swift 3 / 4 get current location with location manager .

3
Nov,2017

0

Step – 1 : 

First define LocationManager . And Current location of CLLocaion .

 

var manager = CLLocationManager()

var currentLocation: CLLocation!

Step 2 :
In ViewDidController get Current location with location manager . That’s it .

  manager.requestWhenInUseAuthorization()

        

        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||

            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){

            currentLocation = manager.location

            print("location is ", currentLocation.coordinate.latitude)

            print("location is ", currentLocation.coordinate.longitude)

            

            lat = currentLocation.coordinate.latitude

            lon = currentLocation.coordinate.longitude

            

            

        }

        

Here the complete code –

   var manager = CLLocationManager()

    var currentLocation: CLLocation!

      override func viewDidLoad() {

        super.viewDidLoad()

            manager.requestWhenInUseAuthorization()

          if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||

            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){

            currentLocation = manager.location

            print("location is ", currentLocation.coordinate.latitude)

            print("location is ", currentLocation.coordinate.longitude)

            

            lat = currentLocation.coordinate.latitude

            lon = currentLocation.coordinate.longitude

            

            

        }

}



Leave a Reply

Your email address will not be published. Required fields are marked *