Naveen Saini
Naveen Saini

Reputation: 81

How to pass value from one function to another?

I want to pass my current location coordinates from func locationManager() to my func viewDidLoad() how can I pass that ?

let manager = CLLocationManager()
var myLocation: CLLocationCoordinate2D?
var location=0
var latPass: Double!
var longPass: Double!

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    var location=locations[0]
    let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
    var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
    latPass=location.coordinate.latitude
    longPass=location.coordinate.longitude
}

When I print my lat & long in the func locationManager I get the value, but when I print it in my viewDidLoad(), I get nil

Here is my viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    print(latPass)
    print(longPass)

    let position = CLLocationCoordinate2D(latitude:latPass, longitude: longPass)
    let london = GMSMarker(position: position)
    london.title = "You"
    london.tracksViewChanges=true
    london.icon = UIImage(named: "currentLocation.png")
    london.map = mapView
    mapView.selectedMarker=london
}

I think I have to make a instance of the var, but I'm unable to find a way.

Upvotes: 1

Views: 75

Answers (2)

Gourav Joshi
Gourav Joshi

Reputation: 2419

ViewDidLoad it is the first method called by view Controller and once it is called the other methods are also called. You need to set delegate in viewDidLOad class. These params during loading of view remains empty, but when the delegate method called it will store data and you will get it as per in method.

You need like this:

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
}

And then this method executes its stuff:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    var location=locations[0]
    let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
    var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
    latPass=location.coordinate.latitude
    longPass=location.coordinate.longitude
    print(latPass)
    print(longPass)
    /// Here you can set the positioning managing stuff, because lat long are showing.
   let position = CLLocationCoordinate2D(latitude:latPass, longitude: longPass)
   let london = GMSMarker(position: position)
   london.title = "You"
   london.tracksViewChanges=true
   london.icon = UIImage(named: "currentLocation.png")
   london.map = mapView
   mapView.selectedMarker=london
}

Once method called you can see console for outputs.

Upvotes: 1

Govaadiyo
Govaadiyo

Reputation: 6082

As per your code you can't pass values to your viewDidLoad method like that because of viewDidLoad method call when loading of your view is finished and didUpdateLocations it's delegate method so it will take some time to call that's why you got null.

Still can't understand what you want to active. More elaborate with code so will help you.

Upvotes: 0

Related Questions