CLLocationManager的委托(delegate)方法
didChangeAuthorizationStatus 和 didUpdateToLocation
没有被调用。
Location Always Usage Description 键已添加到 info.plist 中,我在第一次启动应用程序时也会收到通知。
我可以看到谷歌地图,但我看不到当前位置,当我改变位置时,它没有得到更新。 Basicaaly 委托(delegate)方法未被调用。
//代码
import UIKit
import GoogleMaps
class ViewController: UIViewController,GMSMapViewDelegate {
@IBOutlet weak var mapViewTest: GMSMapView!
let locationManager = CLLocationManager()
var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
override func viewDidLoad()
{
super.viewDidLoad()``
locationManager.delegate = self
if (CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
// Do any additional setup after loading the view, typically from a nib.
}
}
extension ViewController : CLLocationManagerDelegate
{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .authorizedAlways
{
if(CLLocationManager .locationServicesEnabled())
{
locationManager.startUpdatingLocation()
mapViewTest.isMyLocationEnabled = true
mapViewTest.settings.myLocationButton = true
}
}
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
{
mapViewTest.camera = GMSCameraPosition(target: (newLocation.coordinate), zoom: 15, bearing: 0, viewingAngle: 0)
currentLocation = newLocation
currentLatitude = newLocation.coordinate.latitude
currentLongitude = newLocation.coordinate.longitude
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
最佳答案
从您的代码中,您使用的是 Swift 3,并且在 Swift 3 中,CLLocationManagerDelegate
方法的签名已更改为这样。
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
//func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation,
from oldLocation: CLLocation) is deprecated with below one
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
在 CLLocationManagerDelegate
上查看 Apple 文档了解更多详情。
关于ios - CLLocationManager 委托(delegate)方法没有被调用(集成了谷歌地图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40718552/