为什么我的mapView(GMSMapView)为零?

时间:2017-06-16 13:04:34

标签: ios swift

有人可以帮帮我吗? 我从一个视图(verifyCode)转到另一个视图(mainMap)。当我转到mainMap时,我收到一条错误,说mapView是零。为什么会这样?

以下是一些代码: mainMapController

@IBOutlet weak var mapView: GMSMapView!


override func viewDidLoad() {

    super.viewDidLoad()
    let camera = GMSCameraPosition.camera(withLatitude: 38.96351774, longitude:-77.01957901, zoom: 15.0)
    mapVIEW.camera = camera <-------- it errors on this line.
}

verifyCodeController:

if code == codeField.text{
        numberInUse(number: phoneNumber!, completionHandler: { (Value) in
            if Value == true{
                print("toMap segue")
                print(Value)
                self.performSegue(withIdentifier: "nextMap", sender: nil)
            }

1 个答案:

答案 0 :(得分:2)

Documentation显示了如何初始化地图

// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView

您的代码中没有任何内容可以暗示您已经初步确定了它

你至少需要打电话

let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

更新:

确保在App Startup上设置API密钥:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    GMSServices.provideAPIKey("YOUR_API_KEY")
    return true
}