Google API地图无法显示在TabBar应用程序

时间:2015-11-23 13:58:14

标签: swift tabbarcontroller

我正在学习Google地图,它在SingleView应用程序中运行良好。 但是我想改进我的应用并使用了Tab Bar菜单。我将我的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
    }
}

1 个答案:

答案 0 :(得分:0)

我想我发现了问题,至少现在一切正常。 所以我搬了

mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
<{>} func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])之前的locationManager.stopUpdatingLocation()现在可以正常工作。

但是我觉得我的代码很糟糕,它使用了stopUpdatingLocation()两次。在我的情况下是否意味着,该应用程序在应用程序提出时只会获得一次用户位置?