Google 地图 iOS SDK
Google Maps iOS SDK
这是我的代码
import UIKit
import GooglePlaces
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GMSPlacesClient.provideAPIKey("************************")
GMSServices.provideAPIKey("************************")
return true
}
但我仍然遇到异常
Terminating app due to uncaught exception 'GMSServicesException',
reason: 'Google Maps SDK for iOS must be initialized via [GMSServices
provideAPIKey:...] prior to use'
还有其他原因吗,帮我解决一下
ViewController class 在 appDelegate 之前调用。因此,APIKey 未启动。找到这个后,我在 appDelegate 中启动 viewController。
您必须在 App delegate 中添加 API Key 并在 ViewController
中实现 GoogleMap delegate
import UIKit
import GoogleMaps
let googleApiKey = "<YOUR API Key HERE>"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GMSServices.provideAPIKey(googleApiKey)
return true
}
//other methods
}
你的ViewController应该是
import UIKit
import GoogleMaps
class ViewController: UIViewController, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
PS: Make sure you don't forget these important steps.
- 转到 Google Developers Console 并创建应用和 API 密钥
- 为 Google 启用 APIs iOS
Maps SDK
- 在.plist文件中添加隐私权限
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Application requires user’s location information while the app is running in the foreground.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
这是我的代码
import UIKit
import GooglePlaces
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GMSPlacesClient.provideAPIKey("************************")
GMSServices.provideAPIKey("************************")
return true
}
但我仍然遇到异常
Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'
还有其他原因吗,帮我解决一下
ViewController class 在 appDelegate 之前调用。因此,APIKey 未启动。找到这个后,我在 appDelegate 中启动 viewController。
您必须在 App delegate 中添加 API Key 并在 ViewController
中实现 GoogleMap delegateimport UIKit
import GoogleMaps
let googleApiKey = "<YOUR API Key HERE>"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GMSServices.provideAPIKey(googleApiKey)
return true
}
//other methods
}
你的ViewController应该是
import UIKit
import GoogleMaps
class ViewController: UIViewController, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
PS: Make sure you don't forget these important steps.
- 转到 Google Developers Console 并创建应用和 API 密钥
- 为 Google 启用 APIs iOS Maps SDK
- 在.plist文件中添加隐私权限
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Application requires user’s location information while the app is running in the foreground.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>