使用 Google Places API,我可以在 map 上搜索地点并获取有关它的信息(例如网站),使用此功能,从导航栏中的搜索按钮调用 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)
,它是 nil - 所以我认为我没有正确设置变量,但我不确定为什么。
如何正确设置 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
}
然后,一旦出现信息窗口,我想点击它并转到该网站 - 同样,默认情况是我尝试使用 self.selectedPlace?
的位置值(value)观:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
let placeName = marker.title!
switch placeName {
case "George's":
if let georgesUrl = URL(string: "http://www.georges-ny.com") {
self.webView.load(URLRequest(url: georgesUrl))
}
case "TGI Friday's":
if let fridaysUrl = URL(string: "https://www.tgifridays.com") {
self.webView.load(URLRequest(url: fridaysUrl))
}
case "Reserve Cut":
if let rcUrl = URL(string: "http://reservecut.com") {
self.webView.load(URLRequest(url: rcUrl))
}
case "Bill's Bar & Burger":
if let billsUrl = URL(string: "http://www.billsbarandburger.com") {
self.webView.load(URLRequest(url: billsUrl))
}
case "O'Hara's":
if let oharasUrl = URL(string: "http://www.oharaspubnyc.com") {
self.webView.load(URLRequest(url: oharasUrl))
}
default:
// user-selected place
if let site = self.selectedPlace?.website {
self.webView.load(URLRequest(url: site))
}
}
webView.allowsBackForwardNavigationGestures = false
self.view = webView
// return to map
var backButton = UIImage(named: "back")
backButton = backButton?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: backButton, style: UIBarButtonItemStyle.plain, target: self, action: #selector(handleReturn))
navigationItem.title = "Restaurant Webpage"
}
最佳答案
您的问题是您在回调闭包中创建了一个新的 map View ,而不是将地点添加到现有的 map View 中。这个新的 map View 缺少您所有的原始位置,并且它的委托(delegate)未设置为您的 View Controller 实例,因此不会调用委托(delegate)方法。
当您创建 map View 时,您应该将其存储在实例属性中并将新地点添加到该 map View :
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
}
关于ios - 将数据设置为类变量(来自 Google Places API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517360/