-1

I'm starting a location manager as i open the view with this code:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    var userLocation: CLLocation = locations[0] as! CLLocation

    userLatitude = userLocation.coordinate.latitude
    userLongitude = userLocation.coordinate.longitude

    if (userLocation.horizontalAccuracy < 80) {

        manager.stopUpdatingLocation()

    } else {

        addMapRegionAndAnnotation()

    }

}

So this means the locationManager does Stop if the location is detected in a 80m radius. Now i want to start this locationManager again if i press my updateLocation button:

@IBAction func updatePostMapView(sender: AnyObject) {

    manager.startUpdatingLocation()

}

Then the app shuold update the user location till the radius is smaller then 80m again... But as you may expect, this isn't working because i can't call the manager.startUpdatingLocation() from outside of the locationManager.

Has anyone a solution?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

You can call startUpdatingLocation() or stopUpdatingLocation() from inside of any method. First You must create location manager within your class,

class

let locationManager = CLLocationManager()

func initializeLocationManager()
{

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    locationManager.pausesLocationUpdatesAutomatically = false
    locationManager.distanceFilter=kCLLocationAccuracyNearestTenMeters


}
func anyMethod()
{
   locationManager.stopUpdatingLocation()
   locationManager.startUpdatingLocation()
}