0

I have two different classes, one of them, LocationService, is just for determining location. Problem is that I don't know why it doesn't update coordinates variable.

ViewController:

import UIKit

class ViewController: UIViewController {
    var coords: (lat: Double, long: Double) = (0,0)
    var tracking = LocationServices()

    override func viewDidLoad() {
        super.viewDidLoad()

        tracking.startTracking()
        tracking.getLocations()
        coordinat = tracking.coordinates
        retreiveWeatherForecast()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }   
    // retrieveWeatherForecast functions

And simple locationService class:

import UIKit
import CoreLocation

class LocationServices: NSObject, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()
    var coordinates: (lat: Double, long: Double) = (0,0)

    func startTracking() {  

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        locationManager.stopUpdatingLocation()
        print(error)
    }

    func getLocations() {

        #if os (ios)
            func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
                //println("locations = \(locationManager)")
                var latValue = locationManager.location.coordinate.latitude
                var lonValue = locationManager.location.coordinate.longitude
                coordinates = (latValue,lonValue)
                println(latValue)
                println(lonValue)

            }
        #endif
    }
}

When I build it, it returns weather with default coordinates = (0,0).

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Konstantin
  • 197
  • 3
  • 12
  • Why did you put that didUpdateLocations method under getLocations()? – Shoaib Sep 17 '15 at 19:03
  • I was trying to make it work (initialize) before actually starting retrieveWeatherForecast function. – Konstantin Sep 17 '15 at 19:12
  • Compare your code with this http://stackoverflow.com/questions/24252645/how-to-get-location-user-whith-cllocationmanager-in-swift and this http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/ – Shoaib Sep 18 '15 at 09:44
  • Did you add `NSLocationAlwaysUsageDescription` in plist file? – Kosuke Ogawa Sep 19 '15 at 03:04
  • @Shoaib, did you delete your solution with DELEGATES? Can't find it anywhere. I need to make it retrieveWeather function work right after the DidUpdateLocations function. – Konstantin Sep 19 '15 at 08:33
  • yes, i've deleted the answer as you said it was't working. – Shoaib Sep 19 '15 at 19:13

1 Answers1

0

Well, I accidentally made it work. As far as I understand problem was here: func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) locations must be of CLLocation type, not AnyObject. I'm using Swift 2.0.

Konstantin
  • 197
  • 3
  • 12