首先:我知道 So 上有类似的主题,但没有任何建议对我有用。
这是我正在尝试做的: 我想要一个 PositionController(Class) 来控制用户的当前位置并为其他类、ViewController 等提供接口(interface)以使用这些信息。
目前我已经实现了这段代码:
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)
}
}
请注意,委托(delegate)函数中的打印语句目前只是占位符。 另请注意,我希望从 ViewController 外部调用 getPosition-Method。但是当我调用该函数时,它只是通过代码块而不调用最后两个委托(delegate)函数中的任何一个。
此外,我还有“隐私 - 使用时的位置说明”和 “位置使用说明”添加到我的 plist。 我真的不知道我错过了什么或做错了什么,所以感谢所有帮助
编辑:
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
}
最佳答案
您的问题是您正在创建 PositionController
的实例作为 viewDidLoad
中的局部变量。 viewDidLoad
将退出,在位置回调有机会执行之前释放 PositionController
实例(确定位置可能需要几秒钟甚至更长的时间)。
如果您使用属性来保存您的 PositionController
引用,那么它将与您的 View Controller 对象具有相同的生命周期:
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
}
您可能希望在您的应用程序中有一个 PositionController
实例。您可以将它作为您的 AppDelegate
的属性,或者使其成为一个独立的单例(单例可能会在可模拟性和测试方面产生问题,所以我将由您来决定您想要做什么它)。
关于ios - requestLocation 没有调用 didUpdateLocations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44359281/