First of all: I know there are similar threads here on So, but nothing thats suggested works for me.
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.
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. Also note that i want the getPosition-Method to be called from outside ViewControllers. 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
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).
If you use a property to hold your PositionController
reference then it will have the same lifetime as your view controller object:
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. 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).
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.