swift - 谷歌地图不允许我定位用户位置?
问题描述
我正在尝试在 swift 上创建一个视图控制器,以显示用户所在的位置。我已经实现了谷歌地图,所以现在我要做的就是插入正确的代码。这样做时,我不断收到这两条错误消息,然后应用程序崩溃。有人可以帮我找出解决方案吗>感谢所有帮助。
import UIKit
import Foundation
import Firebase
import MapKit
import GoogleMaps
import CoreLocation
class mainViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
let defaults = UserDefaults.standard
let locationManager = CLLocationManager()
var mapView = GMSMapView()
var camera = GMSCameraPosition()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
GMSServices.provideAPIKey("AIzaSyBDOLisA3c-wDTbkbSssAxEb3iLw7Y5vHo")
let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!, zoom: 17)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!)
marker.snippet = "Current Location"
marker.map = mapView
self.mapView.addSubview(mapView)
view.backgroundColor = GREEN_Theme
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Welcome"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(Logout))
}
@objc func Logout() {
print("Logged Out")
do {
// I am receiving this error message on the auth.auth().signOut() "Use of unresolved identifier 'Auth'"
try Auth.auth().signOut()
defaults.set(false, forKey: "user is logged in")
let loginController = UINavigationController(rootViewController: LoginController())
present(loginController, animated: true, completion: nil)
} catch let err {
print(err.localizedDescription)
}
}
}
解决方案
您的问题是CLLocationManager
没有足够的时间来获取信息,同时其他功能要求该信息仍然为零。
下面会解决这个问题,它还会一直停止更新位置,这可能会耗尽电池电量,特别是考虑到您已设置 AccuracyBest。
func getLocation(){
locationManager=CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let lastLocation=locations[locations.count-1]
if lastLocation.horizontalAccuracy>0{
locationManager.stopUpdatingLocation()
let latitude = lastLocation.coordinate.latitude
let longitude = lastLocation.coordinate.longitude
GMSServices.provideAPIKey("AIzaSyBDOLisA3c-wDTbkbSssAxEb3iLw7Y5vHo")
// everything that is going to require the latitude and longitude from the location manager goes here
let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!, zoom: 17)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!)
marker.snippet = "Current Location"
marker.map = mapView
self.mapView.addSubview(mapView)
}
}
你viewDidLoad
应该有:
override func viewDidLoad() {
super.viewDidLoad()
getLocation()
view.backgroundColor = GREEN_Theme
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Welcome"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(Logout))
}
推荐阅读
- c# - C#访问项目文件夹内的图像
- asp.net-mvc - 提交模态引导程序时如何重定向到特定页面?
- php - 返回 Laravel API 资源中的图像路径
- javascript - 为 MiddleWare 路由器添加从客户端到 NodeJS 服务器的请求变量
- typo3 - TYPO3:从 TYPO3 6.2 LTS 到 TYPO3 9 LTS 从 mininews 到 news 的 SQL 迁移
- spring-boot - Spring Boot 测试不适用于 Java 11
- c# - 在 IQueryable 中按小时分组
- c# - 用 TAKE 查询并检查它是否为空
- cassandra - 理解 Cassandra Paxos 的实现
- go - 检测操作系统版本