我的目标是将相机移动到用户的当前位置,但是为了某种方式它继续显示一般地图,我尝试了很多东西,但似乎它不会移动到用户的当前位置
截图
当前代码
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()
}
}
}
答案 0 :(得分:3)
您是否在NSLocationAlwaysUsageDescription
文件中添加了NSLocationWhenInUseUsageDescription
或.plist
(在您的情况下是这一个)密钥。因为如果要求授权的警报视图未显示,则可能是问题所在。
答案 1 :(得分:0)
确保您已正确导入并注册了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也是如此。在加载时,您应该检查是否设置了权限,然后从那里开始。
答案 2 :(得分:0)
首先,您需要设置以下基本步骤:
第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步:要显示当前位置,只需调用this方法。
self.showCurrentLocation()