[英]requestLocationisn't calling didUpdateLocations
First of all: I know there are similar threads here on So, but nothing thats suggested works for me. 首先:我知道So上有类似的主题,但是那没什么建议对我有用。
Here is what i am trying to do: I want to have a PositionController(Class) that controls the users current position and gives interfaces for other classes,ViewControllers etc to use the information. 这是我正在尝试做的事情:我想拥有一个PositionController(Class)来控制用户的当前位置,并为其他类,ViewControllers等提供接口以使用该信息。
Currently i have this code implemented: 目前我已实现此代码:
import Foundation
import CoreLocation
class PositionController: CLLocationManager, CLLocationManagerDelegate{
let locationManager: CLLocationManager
override init() {
self.locationManager = CLLocationManager()
print("---------------Init---------------------------")
}
func getPosition(){
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func locationManager(_ mnager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
}
Note that the print statements in the delegate functions are only placeholders at the moment. 请注意,委托函数中的print语句目前仅是占位符。 Also note that i want the getPosition-Method to be called from outside ViewControllers. 还要注意,我希望从ViewControllers外部调用getPosition-Method。 But when i call the function it just goes through the codeblock without calling neither of the two delegate functions at the end. 但是,当我调用该函数时,它只是通过代码块,而没有最后调用两个委托函数。
Further i have "Privacy - Location When In Use Description" and "Location Usage Description" added to my plist. 此外,我在个人列表中添加了“隐私-使用中的位置描述”和“位置使用情况描述”。 I really dont know what i am missing or doing wrong, so all help is appreciated 我真的不知道我在想什么或做错了什么,因此感谢所有帮助
EDIT: 编辑:
import UIKit
import GoogleMaps
import GoogleMaps
class MapViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// For use in foreground
print("-----------------------In------------------------")
let positionController = PositionController()
positionController.getPosition()
print("-----------------------Out------------------------")
let camera = GMSCameraPosition.camera(withLatitude: 48.137154
longitude: 11.576124, zoom:12)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Munich"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
self.view = mapView
}
Your problem is that you are creating an instance of PositionController
as a local variable in viewDidLoad
. 您的问题是要在viewDidLoad
中将PositionController
的实例创建为局部变量。 viewDidLoad
will exit, deallocating the PositionController
instance before the location callback gets a chance to execute (Determine location can take quite a few seconds or even longer). viewDidLoad
将退出,并在位置回调有机会执行之前重新分配PositionController
实例(确定位置可能要花费几秒钟甚至更长的时间)。
If you use a property to hold your PositionController
reference then it will have the same lifetime as your view controller object: 如果您使用一个属性来保存PositionController
引用,则该引用将与您的视图控制器对象具有相同的生存期:
class MapViewController: UIViewController, CLLocationManagerDelegate {
let positionController = PositionController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// For use in foreground
print("-----------------------In------------------------")
self.positionController.getPosition()
print("-----------------------Out------------------------")
let camera = GMSCameraPosition.camera(withLatitude: 48.137154
longitude: 11.576124, zoom:12)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Munich"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
self.view = mapView
}
You probably want to have a single instance of PositionController
in your app. 您可能希望在应用程序中有一个PositionController
实例。 You could make it a property of your AppDelegate
or make it a singleton in its own right (Singletons can create issues with mockability and testing, so I will leave it up to you to decide how you want to do it). 您可以将其设置为AppDelegate
的属性,也可以将其单独设置为单例(Singletons可以创建具有可模拟性和测试性的问题,因此我将由您自己决定要如何做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.