Google API 地图未显示在 TabBar 应用程序中
Google API Maps don't show up on TabBar Application
我正在学习 Google 地图,它在 SingleView 应用程序中完美运行。
但是我想改进我的应用程序并使用了标签栏菜单。我将 ViewController 与 MapView 连接到 TabBarController,然后地图加载失败,只有空白方块而不是地图。
我做错了什么?
编辑:
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
self.locationManager.startMonitoringSignificantLocationChanges()
}
//MARK: Shows Google Map
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.locationManager.stopUpdatingLocation()
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
mapView.camera = GMSCameraPosition(target: coord, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
我想我找到了问题所在,至少现在一切正常。
所以我搬家了
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
在 locationManager.stopUpdatingLocation()
之前 到 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
现在可以了。
但是我确实觉得我的代码很糟糕,它使用了 stopUpdatingLocation()
两次。就我而言,这是否意味着该应用程序只会在应用程序加载时获取用户位置一次?
我正在学习 Google 地图,它在 SingleView 应用程序中完美运行。 但是我想改进我的应用程序并使用了标签栏菜单。我将 ViewController 与 MapView 连接到 TabBarController,然后地图加载失败,只有空白方块而不是地图。
我做错了什么?
编辑:
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
self.locationManager.startMonitoringSignificantLocationChanges()
}
//MARK: Shows Google Map
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.locationManager.stopUpdatingLocation()
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
mapView.camera = GMSCameraPosition(target: coord, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
我想我找到了问题所在,至少现在一切正常。 所以我搬家了
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
在 locationManager.stopUpdatingLocation()
之前 到 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
现在可以了。
但是我确实觉得我的代码很糟糕,它使用了 stopUpdatingLocation()
两次。就我而言,这是否意味着该应用程序只会在应用程序加载时获取用户位置一次?