ios - 我的应用程序在尝试设置我的 map View 时崩溃

标签 ios swift google-maps storyboard

我正在尝试将样式化的 Google map 集成到我使用 Swift 编写的 iOS 应用程序中。我的 Storyboard 中有一个 GMSMapView View ,我正在尝试使用自定义 JSON 为其着色。下面是制作 mapView 的代码:

@IBOutlet weak var mapView: GMSMapView!

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.camera(withLatitude: userLocation!.coordinate.latitude,
                                          longitude: userLocation!.coordinate.longitude, zoom: 13.0)

    mapView.isMyLocationEnabled = true
    mapView.camera = camera
    self.view = mapView

    locationManager.stopUpdatingLocation()
}

override func loadView() {
    do {
        // Set the map style by passing the URL of the local file.
        if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
***error->            self.mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
        } else {
            NSLog("Unable to find style.json")
        }
    } catch {
        NSLog("One or more of the map styles failed to load. \(error)")
    }
}

但是当我尝试运行它时,出现 fatal error :

unexpectedly found nil while unwrapping an Optional value

引发错误 的行是带有*** 的行。我已将 GMSMapView 与 Storyboard 上的 View 链接起来,应用程序可以正确编译,而无需尝试对其进行样式设置。有谁知道为什么会发生此错误?我用 Google 搜索了错误,但找不到与我的代码相关的任何内容,而且我无法理解某些链接要我做什么。

最佳答案

错误是因为self.mapViewnil .原因如下:

您的 map View 被设置为 Storyboard的导出。在这种情况下,将为您创建 map View 。只需确保 map View 实际连接到 socket 即可。

真正的问题是你已经重载了 loadView方法。不要那样做。来自 UIViewController loadView 的文档:

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

您当前在 loadView 中的代码应该移到 viewDidLoad方法。

override func viewDidLoad() {
    super.viewDidLoad()

    do {
        // Set the map style by passing the URL of the local file.
        if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
            self.mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
        } else {
            NSLog("Unable to find style.json")
        }
    } catch {
        NSLog("One or more of the map styles failed to load. \(error)")
    }

    // and any other code you might need in viewDidLoad
}

下一个大问题是给 self.view 赋值在位置管理器委托(delegate)中。那也很糟糕。您唯一应该分配给 self.view 的地方在 View Controller 中是 loadView并且仅当您以编程方式创建整个 View Controller 及其 View 时。

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.camera(withLatitude: userLocation!.coordinate.latitude,
                                          longitude: userLocation!.coordinate.longitude, zoom: 13.0)

    mapView.isMyLocationEnabled = true
    mapView.camera = camera

    locationManager.stopUpdatingLocation()
}

总结:

  1. 删除 loadView方法。将其内容移至 viewDidLoad
  2. 不要分配给 self.view代码中的任何位置。
  3. 确保 Storyboard中的 map View 真正连接到您的商店。

关于ios - 我的应用程序在尝试设置我的 map View 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794959/

相关文章:

java - 继续获取 {"status":21002} while verifying Apple In-App Purchase receipt

c++ - OpenCV从2D像素获取3D坐标

ios - 核心数据私有(private)上下文 performAndWait 导致调试崩溃

ios - contentOffsetForScrollingToRowAtIndexPath :atScrollPosition:usingPresentationValues

ios - 如何在 Storyboard 中将 View 移动到另一个 View 内

objective-c - 无需导航 Controller 即可切换 View

iphone - 将导航 Controller 添加到 UIViewController

Android更新textview计时器并刷新 map 内容?

image - 检测谷歌地图街景静态图片的 "we have no imagery"

google-maps - 谷歌地图可以作为Mapbox-gl基础层连接吗?