简体   繁体   中英

What is calling didUpdateLocations in my Swift code?

I am learning Swift, and in the snippet code below there is a locationManager method. Why should I write this even though it has not been called in the action code of the Locate me button?

import UIKit
import MapKit
import CoreLocation

class ViewController: `UIViewController, CLLocationManagerDelegate` {
  @IBOutlet weak var Mapview: MKMapView!
  var manager = CLLocationManager()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(51.5078788, -0.08773210000003928)
    let objectAnn = MKPointAnnotation()
    objectAnn.coordinate = pinLocation
    objectAnn.title = "London Bridge"
    objectAnn.subtitle = "London, United kingdom"
    self.Mapview.addAnnotation(objectAnn)
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  @IBAction func Directions(sender: AnyObject) {
    UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/maps?daddr=51.5078788,-0.08773210000003928")!)
  }

  @IBAction func LocateMe(sender: AnyObject) {
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    Mapview.showsUserLocation = true
  }

  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userlocation:CLLocation = locations[0] as CLLocation

    manager.stopUpdatingLocation()

    let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
    let span = MKCoordinateSpanMake(0.5, 0.5)
    let region = MKCoordinateRegion(center: location, span: span)

    Mapview.setRegion(region, animated: true)
  }
}

The code in question is this:

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

I realise you're learning Swift right now, so it might be strange to have code that you don't call yourself. But in this example, the code gets called by iOS.

You see, when you write manager.startUpdatingLocation() it has the effect that iOS wants to start telling you when the user's location has changed. It does that by looking to see if you have the didUpdateLocations method above. If you have that method in your code, iOS will call it for you – it's not one you will call yourself.

It is called delegate.

you are implementing CLLocationManagerDelegate .

Since you are setting your manager delegate to the view controller by using this code manager.delegate = self it will automatically called delegate event in certain condition.

In your code, if your location is updated, the view controller will automatically run this function func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

In your function LocateMe(sender:) you are calling manager.startUpdatingLocation() this tells the CLLocationManager to start looking for your (i guess) gps coordinates. However now every time your location is being updated this CLLocationManager will call the function locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation] . This is because the updates are asynchronous, and once your location was updated, you are then notified with this function.

In the LocateMe method you are setting the delegate of the locationManager to your controller.

the func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) method is called when the location changes.

For further information check this: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didUpdateLocations :

我认为您必须在.plist中添加NSLocationWhenInUseUsageDescription,然后设置位置并尝试执行此操作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM