[英]What is calling didUpdateLocations in my Swift code?
I am learning Swift, and in the snippet code below there is a locationManager
method. 我正在学习Swift,下面的代码段中有一个
locationManager
方法。 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. 我意识到您正在学习Swift,所以拥有不自称为代码的代码可能很奇怪。 But in this example, the code gets called by iOS.
但是在此示例中,该代码被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. 您会看到,当编写
manager.startUpdatingLocation()
它的效果是iOS希望在用户位置更改时开始告诉您。 It does that by looking to see if you have the didUpdateLocations
method above. 通过查看是否具有上面的
didUpdateLocations
方法来做到这一点。 If you have that method in your code, iOS will call it for you – it's not one you will call yourself. 如果您的代码中包含该方法,iOS会为您调用该方法-您不会自己调用它。
It is called delegate. 它称为委托。
you are implementing CLLocationManagerDelegate
. 您正在实现
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. 由于使用此代码
manager.delegate = self
将manager
委托设置为视图控制器,因此它将在特定条件下自动调用委托事件。
In your code, if your location is updated, the view controller will automatically run this function func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
在您的代码中,如果您的位置已更新,则视图控制器将自动运行此函数
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. 在函数LocateMe(sender :)中,您正在调用
manager.startUpdatingLocation()
它告诉CLLocationManager
开始查找您的(我想)gps坐标。 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. 但是,现在每次您的位置被更新时,此
CLLocationManager
都会调用函数locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]
。这是因为更新是异步的,并且一旦您的位置被更新,便会通过此函数通知您。
In the LocateMe
method you are setting the delegate
of the locationManager to your controller. 在
LocateMe
方法中,您将locationManager的delegate
设置为您的控制器。
the func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
method is called when the location changes. 位置更改时,将调用
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
方法。
For further information check this: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didUpdateLocations : 有关更多信息,请检查: https : //developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager : didUpdateLocations :
我认为您必须在.plist中添加NSLocationWhenInUseUsageDescription,然后设置位置并尝试执行此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.