我实施了Google文档中的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 {
self.navigationItem.title = place.name
if let website = place.website {
self.webView.load(URLRequest(url: website))
self.view = self.webView
print(website)
}
let camera = GMSCameraPosition.camera(withLatitude: 40.708637, longitude: -74.014839, zoom: 18)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
} else {
self.navigationItem.title = "No place selected"
}
})
}
并添加到if let website = place.website
条件内的webView请求中。当我运行应用程序并选择一个位置时,print语句使用正确的URL运行,但webView不会加载。谁能看到我做错了什么?
最终,我希望能够在pickPlace函数之外使用该网站,并在我的didTapWindowOf
函数中使用它。也许通过初始化像var site: URL?
这样的变量并将place.website(unwrapped)设置为该变量。然后在didTapWindowOf
我只使用self.webView.load(URLRequest(url: site))
。但我相对较新,这种做法对我不起作用,所以任何帮助都会受到赞赏!