【问题标题】:Swift: Google Map code throwing nil exception斯威夫特:谷歌地图代码抛出零异常
【发布时间】:2015-09-17 17:30:07
【问题描述】:

我有以下代码,它应该基本上检测位置(如果位置服务已打开),然后返回上一页并显示坐标(单击按钮时)。

@IBOutlet var mapView:GMSMapView!// 在 Storyboard 上添加谷歌地图检查 var locationManager:CLLocationManager! var viewController : 视图控制器! //保存前一个控制器的对象以返回位置

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    locationManager = CLLocationManager() // Intialize the location manager
    locationManager.delegate = self

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//Methos to change the MApType
@IBAction func changeMaptoEarthView()
{
    self.mapView.mapType = kGMSTypeSatellite
}

@IBAction func changeMaptoMapView()
{
    self.mapView.mapType = kGMSTypeNormal
}
//Method to get UserLocation
@IBAction func sendLocationtoPreviousView()
{
    viewController.locationRecieved(locationManager.location)
    self.performSegueWithIdentifier("backSegue", sender: nil)
}

@IBAction func addUserLocation() //Detect location click event
{
    if(CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse) // If already have location access
    {
        locationManager.startUpdatingLocation()
    }
    else if (CLLocationManager.authorizationStatus() == .Denied)// If location access is denied in setting
    {
        UIAlertView(title: "Permission", message: "Please allow location services in setting", delegate: nil, cancelButtonTitle: "ok").show()
    }
    else
    {
        locationManager.requestWhenInUseAuthorization() // Ask user permission if permission not given
    }
}

//CLLocation Manager Delegate methoda
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    if status == .AuthorizedWhenInUse {

        locationManager.startUpdatingLocation()

    }
}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    if let location = locations.first as? CLLocation {

        self.mapView.clear() // clear all overlay on Map
        self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
        // Adding pin on Current location
        var marker = GMSMarker()
        marker.position = location.coordinate
        marker.snippet = "My Location"
        marker.appearAnimation = kGMSMarkerAnimationPop
        marker.map = mapView

        locationManager.stopUpdatingLocation()
    }
}

应用程序在模拟器中运行,直到我说返回,然后它在这一行抛出 nil 错误消息:

    viewController.locationRecieved(locationManager.location)

可能是什么原因?我可以在地图上看到图钉,虽然地图没有显示任何图形,除了下面的谷歌徽标

【问题讨论】:

    标签: ios swift google-maps


    【解决方案1】:

    如果您在视图(GMSMapView 的子类)中看不到地图图形,这是因为 Google 地图 API 密钥,可能您尚未在应用程序委托中设置它或未正确设置它!

    确保您已使用您的 app bundle id 和您在 google 中的帐户设置了正确的 google API 密钥。您必须在您的应用程序中设置以下代码删除,didFinishLaunchingWithOptions 功能:

    import GoogleMaps
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
              GMSServices.provideAPIKey("your API Key")
    }
    

    【讨论】:

      【解决方案2】:

      CLLocationManager 上的 location 属性是 Optional - 这意味着它可以为 nil。根据文档,如果没有检索到任何位置数据,它将为零。

      您没有显示viewController.locationRecieved 是如何定义的,但我猜您已将CLLocation 参数配置为隐式展开的可选参数(即,使用“!”)。将 nil 传递给这样的参数将导致崩溃。

      【讨论】:

      • 是的,这就是它的原因,但仍然无法解决
      • 更改“!”到 ”?”对于 viewController.locationRecieved 的参数并测试该方法内的 nil 情况。
      • 我还是不明白为什么它是 nil。
      • 根据文档,如果由于某种原因没有检索到位置数据,它将为零。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多