-2

I am developing an app using swift I want to fetch current location of the user and want to show in google map so I wrote my code in my class I have included all the functions and methods (i.e) added and imported frameworks core location and also updated plist but I can't able to fetch current location rotationally some time I get the location but some time it crashed.

Here is the code what I am tried.

import CoreLocation
class HomeViewController: UIViewController, CLLocationManagerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.startUpdatingLocation()

    let location: CLLocation? = locationManager.location
    let coordinate: CLLocationCoordinate2D? = location?.coordinate  ----> App crossed this line then it will crashed
    print(coordinate!)
    print(coordinate!.latitude)
    print(coordinate!.longitude)
    strForCurLatitude = "\(coordinate!.latitude)"
    strForCurLongitude = "\(coordinate!.longitude)"}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse {
        print("User allowed us to access location")
    }
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error while get location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locationManager.startUpdatingLocation()
    let locationNew = locations.last
    let coordinateNew: CLLocationCoordinate2D? = locationNew?.coordinate
    strForCurLatitude = "\(coordinateNew!.latitude)"
    strForCurLongitude = "\(coordinateNew!.longitude)"
    strForLat=strForCurLatitude;
    strForLong=strForCurLongitude;
    print("User's Latitude is: \(Double(strForLat!)!)")
    print("User's Longitude is: \(Double(strForLong!)!)")
    self.locationManager.startUpdatingLocation()
}

2 Answers2

0

Remove this from ViewDidLoad because you can not get location in viewDidLoad.

let location: CLLocation? = locationManager.location
    let coordinate: CLLocationCoordinate2D? = location?.coordinate 
    print(coordinate!)
    print(coordinate!.latitude)
    print(coordinate!.longitude)
    strForCurLatitude = "\(coordinate!.latitude)"
    strForCurLongitude = "\(coordinate!.longitude)"

In CLLocationManager's following method best for getting location

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

make a reference to CLLocationManager as variable, like this:

class HomeViewController {

    var locationManager = CLLocationManager()

    viewDidLoad() {
       super.viewDidLoad()
    }
    // rest of code
}

in this case Location Manager should works properly.

PiterPan
  • 1,760
  • 2
  • 22
  • 43