Getting Longitude/Latitude of the User When the viewDidLoad

后端 未结 5 579
故里飘歌
故里飘歌 2020-12-29 10:22

My purpose is to get the longitude/latitude of the user when the view is loaded and to store the values in two variables for later calculations. I don\'t need to track the u

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 10:40

    As soon as you call [locationManager startUpdatingLocation]; your viewDidLoad method is done. You need to implement

    -(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation 
               fromLocation: (CLLocation *) oldLocation
    

    and

    - (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    

    which will be called by your locationManager when it gets the location, or when it fails. In both of those methods, you can call [locationManager stopUpdatingLocation].

    Inside the didUpdateToLocation method, you should move all your code from viewDidLoad, starting with CLLocation *location = [locationManager location]; That's where you'll get proper values.

提交回复
热议问题