0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.camera(withLatitude: userLocation!.coordinate.latitude,
                                          longitude: userLocation!.coordinate.longitude, zoom: 13.0)
    mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    self.view = mapView

    locationManager.stopUpdatingLocation()

    let Lat = "\(userLocation!.coordinate.latitude)"
    let Long = "\(userLocation!.coordinate.longitude)"

}

How do I extract the values of Lat and Long out of the function? I've attempted a return and also tried sending the values to the app delegate to be later called back but nothing worked. The values get passed to the app delegate but are not able to be called back into the same VC they came from. I'm new to swift so I sure there has to be a simpler method of getting them out that I'm overlooking.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Abe
  • 23
  • 3
  • What about saving them in variables in the vc scope? – JoakimE Jul 12 '17 at 05:18
  • have you get lat and long in didUpdateLocations delegate method? – Brijesh Shiroya Jul 12 '17 at 05:22
  • @Joakim I think I attempted that and kept getting blank values. – Abe Jul 12 '17 at 05:34
  • @BrijeshShiroya ya I got the string values but just need to take them out the function – Abe Jul 12 '17 at 05:36
  • @Abe Use singletons or save to user defaults..But singleton will be fine.See [this](https://stackoverflow.com/questions/16077008/locationmanagerdidupdatelocations-always-be-called-several-times/32088366#32088366) – LC 웃 Jul 12 '17 at 06:21

1 Answers1

0

Just define userLocation variable outside the didUpdateLocation function and then update the value inside the didUpdateLocation function like below:

var userLocation: CLLocation?

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.camera(withLatitude: userLocation!.coordinate.latitude,
                                          longitude: userLocation!.coordinate.longitude, zoom: 13.0)
    mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    self.view = mapView
    locationManager.stopUpdatingLocation()
}

func getUpdatedLocation() {
    let Lat = "\(userLocation!.coordinate.latitude)"
    let Long = "\(userLocation!.coordinate.longitude)"
}

You can find out lat & long from anywhere like my getUpdatedLocation() function.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Harish Singh
  • 765
  • 1
  • 12
  • 23