[英]Cant get current location (GPS) on google maps ios sdk
我的目标是将相机移动到用户的当前位置,但是为了某种方式它不断显示一般地图,我尝试了很多东西,但似乎它不会移动到用户的当前位置
截图
目前的代码
import UIKit
import GoogleMaps
class ParcelViewController: UIViewController, GMSMapViewDelegate {
@IBOutlet var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// Mark: -CLLocationManagerDelegate
extension ParcelViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
您是否在.plist
文件中添加了NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
(在您的情况下就是这一个)键。 因为如果要求授权的警报视图未显示,则可能是问题所在。
确保您已正确导入并注册了GoogleMapsAPI密钥:这是在AppDelegate中完成的
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("[your key from Google API]")
return true
}
之后,在ViewController文件中的一些加载函数中:
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
self.map.setRegion(region, animated: true)
请注意:Google Maps API不需要用户自动化来使用地图,但CLLLocationManager也是如此。 在加载时,您应该检查是否设置了权限,然后从那里开始。
首先,您需要设置以下基本步骤:
第1步:将其导入您的控制器类
import CoreLocation
第2步:将此委托添加到您的类文件中
class Your_controller: CLLocationManagerDelegate
第3步:声明以上内容以查看负载
var locationManager = CLLocationManager()
第4步:将此代码添加到viewdidload
方法
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
if CLLocationManager.locationServicesEnabled() {
switch (CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
第5步:在viewdidload方法下添加此代码
func showCurrentLocation() {
mapView.settings.myLocationButton = true
let locationObj = locationManager.location as! CLLocation
let coord = locationObj.coordinate
let lattitude = coord.latitude
let longitude = coord.longitude
print(" lat in updating \(lattitude) ")
print(" long in updating \(longitude)")
let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude)
let marker = GMSMarker()
marker.position = center
marker.title = "current location"
marker.map = mapView
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: Float(zoomLevel))
self.mapView.animate(to: camera)
}
第6步:添加权限您的plist文件
密钥 - “隐私 - 使用时的位置使用说明”
价值 - “此应用需要访问您的位置”
第7步:在硬件设备上运行您的应用程序
步骤8:当您想要显示当前位置时,只需调用此方法即可。
self.showCurrentLocation()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.