ios - 在swift 5中获取位置
问题描述
我意识到还有很多其他页面试图解决CLLocationManager()
在 ios swift 中使用获取用户位置的挑战,但它们对我不起作用,这就是我创建这个问题的原因。我的代码在下面,我相信它正确地实现了CLLocationManagerDelegate
但调用locationManager.startUpdatingLocation()
从不调用该locationManager
函数。这使得谷歌地图简单地打开到一个随机位置。我已经模拟了计划中的某些位置,它们像南非一样工作得很好
但是,当我取消选中模拟时,永远找不到该位置。任何建议或帮助将不胜感激。不确定这是否是我的计算机或代码的问题。
import UIKit
import GoogleMaps
class Map: UIViewController, CLLocationManagerDelegate {
var mapView: GMSMapView?
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 40.7, longitude: -74.0, zoom: 4.0))
view = mapView
if (CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let center = CLLocationCoordinate2D(latitude: locations.last!.coordinate.latitude, longitude: locations.last!.coordinate.longitude)
let camera = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 4.0);
self.mapView?.camera = camera
self.mapView?.isMyLocationEnabled = true
let marker = GMSMarker(position: center)
print("Latitude :\(center.latitude)")
print("Longitude :\(center.longitude)")
marker.map = self.mapView
marker.title = "Current Location"
locationManager.stopUpdatingLocation()
}
}
解决方案
推荐阅读
- java - Java:出现在入门指南中
- javascript - 帆布 - 当我放
- javascript - 如何在并行 Javascript 中运行“x”承诺
- oracle - Sqldeveloper 无法连接 Oracle 数据库(ORA-01017,用户名/密码无效;登录被拒绝)
- r - 基于`..count..`变量的ggplot2中`geom_label`的y值
- angular - 带有 Angular 6 的 Mapbox。“找不到地图容器”错误
- c - 如果使用空值初始化的结构占用空间
- python - 将文本文件放在另一个位置 - selenium
- c# - 我的 Azure 函数中的“myQueueItem”在触发时产生零值
- ios - 配置文件。“MY BUNDLE ID”不包括签名证书 iPhone 发行版:“我的名字”