temp 2806
unknown
swift
3 years ago
2.4 kB
10
Indexable
struct EditProfileLocationPickerRepresentable: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
@Environment(\.presentationMode) var presentationMode
@Binding var location: Location?
func makeUIViewController(context: UIViewControllerRepresentableContext<EditProfileLocationPickerRepresentable>) -> GMSAutocompleteViewController {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = context.coordinator
autocompleteController.configureCustomColors()
// Specify the place data types to return.
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.placeID.rawValue) | UInt(GMSPlaceField.coordinate.rawValue) | UInt(GMSPlaceField.formattedAddress.rawValue))
autocompleteController.placeFields = fields
return autocompleteController
}
func updateUIViewController(_ uiViewController: GMSAutocompleteViewController, context: UIViewControllerRepresentableContext<EditProfileLocationPickerRepresentable>) {
}
class Coordinator: NSObject, UINavigationControllerDelegate, GMSAutocompleteViewControllerDelegate {
var parent: EditProfileLocationPickerRepresentable
init(_ parent: EditProfileLocationPickerRepresentable) {
self.parent = parent
}
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
DispatchQueue.main.async {
//print(place.description.description as Any)
self.parent.location = Location(coordinate: place.coordinate, address: place.name ?? place.formattedAddress ?? "")
self.parent.presentationMode.wrappedValue.dismiss()
}
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
parent.presentationMode.wrappedValue.dismiss()
}
}
}
// usage example in ViewSwiftUIName.swift:
EditProfileLocationPickerRepresentable(location: $viewModel.myLocation)Editor is loading...