我使用的是google-maps-sdk
,我在同一个UITableView
中有一个GMSMapView
和一个viewcontroller
。我将autocomplete search
结果放在tableview
中,我想将地图移动到用户选择的单元格所代表的位置。我正在尝试使用didSelectRowAtIndexPath
方法更新地图,但我一直收到"未解包的nil可选错误。"这非常令人困惑,因为我确实将可选项分配给我尝试打开它的上面一行的值。
我将self.mapview分配给相机时出错。任何帮助/想法将不胜感激。谢谢!
ViewDidLoad
方法
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = controller.searchBar
//self.tableView.tableHeaderView = controller.searchBar
return controller
})()
placesClient = GMSPlacesClient()
var camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
self.mapSubView.camera = camera
self.mapSubView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.mapSubView.myLocationEnabled = true
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapSubView
}
didSelectRowAtIndexPath
方法
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
view.sendSubviewToBack(self.tableView)
var result = searchResults[indexPath.row]
var camera = GMSCameraPosition.cameraWithLatitude(42.37, longitude: -71.109734, zoom: 16)
self.mapSubView.camera = camera
self.mapSubView = GMSMapView.mapWithFrame(self.mapSubView.bounds, camera: self.mapSubView.camera)
}
答案 0 :(得分:0)
现在我清楚地知道你的问题所在:
if let mapSubView2 = self.mapSubView {
mapSubView2.camera = camera
self.mapSubView = mapView
}
这种情况永远不会满足,这就是为什么你的mapSubView是零的。为什么?我认为你打算写一些像
这样的东西if let mapSubView2 = mapView {
mapSubView2.camera = camera
self.mapSubView = mapSubView2
}
但是你可以跳过所有这些检查并直接分配mapSubView如下
self.mapSubView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.mapSubView.myLocationEnabled = true