无法在 google 地图 ios sdk 上获取当前位置 (GPS)
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()
}
}
}
确保您已正确导入和注册 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 地图 API 不需要用户授权即可使用地图,但是,CLLLocationManager 需要。在加载时,您应该检查是否设置了权限,然后从那里开始。
您是否在 .plist
文件中添加了 NSLocationAlwaysUsageDescription
或 NSLocationWhenInUseUsageDescription
(在您的情况下是这个)键。因为如果请求授权的警报视图没有显示这可能是问题所在。
首先您需要设置这些基本步骤:
第 1 步:将此导入您的控制器 class
import CoreLocation
第 2 步:将此代表添加到您的 class 文件
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 文件添加权限
密钥 - "Privacy - Location When In Use Usage Description"
值 - "This app needs access to your location"
第 7 步:运行 您在硬件设备上的应用程序
第八步:当你想显示当前位置时调用这个方法即可。
self.showCurrentLocation()
我的目标是将相机移动到用户的当前位置,但不知何故它一直显示一般地图,我尝试了很多方法但似乎不会移动到用户的当前位置
截图
当前代码
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()
}
}
}
确保您已正确导入和注册 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 地图 API 不需要用户授权即可使用地图,但是,CLLLocationManager 需要。在加载时,您应该检查是否设置了权限,然后从那里开始。
您是否在 .plist
文件中添加了 NSLocationAlwaysUsageDescription
或 NSLocationWhenInUseUsageDescription
(在您的情况下是这个)键。因为如果请求授权的警报视图没有显示这可能是问题所在。
首先您需要设置这些基本步骤:
第 1 步:将此导入您的控制器 class
import CoreLocation
第 2 步:将此代表添加到您的 class 文件
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 文件添加权限
密钥 - "Privacy - Location When In Use Usage Description"
值 - "This app needs access to your location"
第 7 步:运行 您在硬件设备上的应用程序
第八步:当你想显示当前位置时调用这个方法即可。
self.showCurrentLocation()