Google 地图 API (GMS) 地图未显示
Google Maps API (GMS) map doesn't show up
我试图让地图自动跟随用户(位置),但不知何故应用程序崩溃了(没有错误消息)或者当我启动应用程序时地图没有显示。我做错了什么?
我尝试过不使用 locationManager 功能,但确实有效。还有其他方法可以关注用户吗?
class GMSTestViewController: BaseViewController, GMSMapViewDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
//Setup Location Manager
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
//Map type
mapView.mapType = kGMSTypeTerrain
}
override func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 8)
let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = center
marker.title = "Current Location"
marker.snippet = "XXX"
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
我开机的时候是这个样子,然后一直跟着地图(在用户位置)闪烁。
检查这些东西:-
您已将 GMSMapview
@IBOutlet 的委托设置为 self
mapView.delegate = self
您已使用特定密钥更新您的 info.plist 文件
- 您的互联网连接正常。
您已在 AppDelegate
中配置 Google 地图
var googleApiKey = String(_yourAPIkey)
GMSServices.provideAPIKey(googleApiKey)
我认为最好的方法是使用键值观察 (KVO):
override func viewWillAppear(animated: Bool) {
mapView.addObserver(self, forKeyPath: "myLocation", options:0, context:nil)
}
deinit {
mapView.removeObserver(self, forKeyPath:"myLocation", context:0)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if(keyPath! == "myLocation"]) {
let location = [object myLocation]
let target =
CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
mapView.animateToLocation(target)
mapView.animateToZoom(17)
}
}
似乎 google 地图没有出现在您的 view.I 中,您认为您想要获得图像中的视图。
所以我建议你下载Google地图SDK版本below 1.10因为我遇到这个问题太多次了.
我建议您下载google map SDK 1.9.1 for google map from below link
https://developers.google.com/maps/documentation/ios-sdk/releases
只需尝试使用版本 1.9.1 一次。希望这个东西有用
注意:如果您想使用 Google 地图的位置自动完成和其他新服务,请使用更高版本的 Google 地图 SDK
有同样的问题。
通过获取新的 API 键修复。
正如我发现的那样,您需要两个密钥:一个用于 Google 个地点,一个用于 Google 个地图。至少出于测试目的。
Google 文档中没有提到。
我试图让地图自动跟随用户(位置),但不知何故应用程序崩溃了(没有错误消息)或者当我启动应用程序时地图没有显示。我做错了什么?
我尝试过不使用 locationManager 功能,但确实有效。还有其他方法可以关注用户吗?
class GMSTestViewController: BaseViewController, GMSMapViewDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
//Setup Location Manager
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
//Map type
mapView.mapType = kGMSTypeTerrain
}
override func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 8)
let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = center
marker.title = "Current Location"
marker.snippet = "XXX"
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
我开机的时候是这个样子,然后一直跟着地图(在用户位置)闪烁。
检查这些东西:-
您已将
GMSMapview
@IBOutlet 的委托设置为 selfmapView.delegate = self
您已使用特定密钥更新您的 info.plist 文件
- 您的互联网连接正常。
您已在
中配置 Google 地图AppDelegate
var googleApiKey = String(_yourAPIkey) GMSServices.provideAPIKey(googleApiKey)
我认为最好的方法是使用键值观察 (KVO):
override func viewWillAppear(animated: Bool) {
mapView.addObserver(self, forKeyPath: "myLocation", options:0, context:nil)
}
deinit {
mapView.removeObserver(self, forKeyPath:"myLocation", context:0)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if(keyPath! == "myLocation"]) {
let location = [object myLocation]
let target =
CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
mapView.animateToLocation(target)
mapView.animateToZoom(17)
}
}
似乎 google 地图没有出现在您的 view.I 中,您认为您想要获得图像中的视图。
所以我建议你下载Google地图SDK版本below 1.10因为我遇到这个问题太多次了.
我建议您下载google map SDK 1.9.1 for google map from below link
https://developers.google.com/maps/documentation/ios-sdk/releases
只需尝试使用版本 1.9.1 一次。希望这个东西有用
注意:如果您想使用 Google 地图的位置自动完成和其他新服务,请使用更高版本的 Google 地图 SDK
有同样的问题。 通过获取新的 API 键修复。 正如我发现的那样,您需要两个密钥:一个用于 Google 个地点,一个用于 Google 个地图。至少出于测试目的。
Google 文档中没有提到。