使用Google Places API我可以使用此功能在地图中搜索地点并获取相关信息(例如网站),从导航栏navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Search", style: .plain, target: self, action: #selector(pickPlace))
中的搜索按钮调用:
func pickPlace() {
let center = CLLocationCoordinate2D(latitude: 40.708637, longitude: -74.014839)
let northEast = CLLocationCoordinate2D(latitude: center.latitude + 0.001, longitude: center.longitude + 0.001)
let southWest = CLLocationCoordinate2D(latitude: center.latitude - 0.001, longitude: center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
let placePicker = GMSPlacePicker(config: config)
placePicker.pickPlace(callback: {(place, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let place = place {
// Set place to class variable selectedPlace
self.selectedPlace = place
// Add marker & move camera to new place
if self.selectedPlace != nil {
let cam = GMSCameraPosition.camera(withTarget: (self.selectedPlace?.coordinate)!, zoom: 18)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: cam)
let newPlaceMarker = GMSMarker(position: (self.selectedPlace?.coordinate)!)
newPlaceMarker.map = mapView
self.navigationItem.title = self.selectedPlace?.name
print(self.selectedPlace?.website)
newPlaceMarker.title = self.selectedPlace?.name
self.view = mapView
}
} else {
self.navigationItem.title = "No place selected"
}
})
}
在if let place = place
内部我尝试使用var selectedPlace: GMSPlace?
将地点设置为名为self.selectedPlace = place
的类变量。但是,当我尝试打印时,例如print(self.selectedPlace?.website)
,它是零 - 所以我认为我没有正确设置变量,但是我不确定原因。
如何正确地将place
从pickPlace
函数设置为我的类变量selectedPlace
,以便我可以在我的应用中使用它?
编辑:显示我尝试使用
的类变量我试图将自定义信息窗口显示在用户搜索的标记上,而不仅仅是我的默认标记。这是我的customInfoWindow函数 - 注意"默认"条件是用户搜索标记时窗口应出现的位置,以及我尝试使用self.selectedPlace?
name
值的地方
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let customInfoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?[0] as! CustomInfoWindow
let placeName = marker.title!
switch placeName {
case "TGI Friday's":
customInfoWindow.nameLbl.text = "TGI Friday's"
customInfoWindow.detailLabel.text = "Thoroughly Average Chain Restaurant"
customInfoWindow.placeImage.image = UIImage(named: "fridays")
self.navigationItem.title = "TGI Friday's"
case "George's":
customInfoWindow.nameLbl.text = "George's"
customInfoWindow.detailLabel.text = "Old School Diner"
customInfoWindow.placeImage.image = UIImage(named: "georges")
self.navigationItem.title = "George's"
case "Reserve Cut":
customInfoWindow.nameLbl.text = "Reserve Cut"
customInfoWindow.detailLabel.text = "Kosher Steakhouse"
customInfoWindow.placeImage.image = UIImage(named: "reserveCut")
self.navigationItem.title = "Reserve Cut"
case "O'Hara's":
customInfoWindow.nameLbl.text = "O'Hara's"
customInfoWindow.detailLabel.text = "Irish Pub"
customInfoWindow.placeImage.image = UIImage(named: "oharas")
self.navigationItem.title = "O'Hara's"
case "Bill's Bar & Burger":
customInfoWindow.nameLbl.text = "Bill's Bar & Burger"
customInfoWindow.detailLabel.text = "Bar founded by Bill that also has burgers"
customInfoWindow.placeImage.image = UIImage(named: "bills")
self.navigationItem.title = "Bill's Bar & Burger"
default:
customInfoWindow.nameLbl.text = self.selectedPlace?.name
customInfoWindow.placeImage.image = UIImage(named: "noImage")
self.navigationItem.title = self.selectedPlace?.name
}
return customInfoWindow
}
然后,当我显示信息窗口后,我想点击它并被带到网站 - 再次,默认条件是我尝试使用{{1} }值:
self.selectedPlace?
答案 0 :(得分:1)
您的问题是您在回调闭包中创建了一个新的地图视图,而不是将该地点添加到现有的地图视图中。这个新的地图视图缺少所有原始位置,并且它的委托没有设置到您的视图控制器实例,因此不会调用委托方法。
创建地图视图时,应将其存储在实例属性中,并将新位置添加到该地图视图中:
if self.selectedPlace != nil {
let cam = GMSCameraPosition.camera(withTarget: (self.selectedPlace?.coordinate)!, zoom: 18)
let newPlaceMarker = GMSMarker(position: (self.selectedPlace?.coordinate)!)
newPlaceMarker.map = self.mapView
self.mapView.camera = cam
self.navigationItem.title = self.selectedPlace?.name
print(self.selectedPlace?.website)
newPlaceMarker.title = self.selectedPlace?.name
}