我正在尝试将样式化的 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.mapView
是nil
.原因如下:
您的 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()
}
总结:
- 删除
loadView
方法。将其内容移至viewDidLoad
- 不要分配给
self.view
代码中的任何位置。 - 确保 Storyboard中的 map View 真正连接到您的商店。
关于ios - 我的应用程序在尝试设置我的 map View 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794959/