add 'Current location' button at the top of the autocomplete places list

88 Views Asked by At

I have a autocomplete that works fine, but I want to add "set current location" to the autocompleter controller. I ve searched in multiple places but there are none implementation for swiftui, This is my Place picker for autocompleter:

struct PlacePicker: UIViewControllerRepresentable {
        
    func makeCoordinator() -> GooglePlacesCoordinator {
        GooglePlacesCoordinator(self)
    }
    @Environment(\.presentationMode) var presentationMode
    @Binding var address: String
    @Binding var latitude: Double
    @Binding var longitude: Double
    
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<PlacePicker>) -> GMSAutocompleteViewController {
        
        GMSPlacesClient.provideAPIKey("xxxxxxxxxxxxxxxxxxxxx")

        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = context.coordinator
        

        let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.name.rawValue) |
                                                                  UInt(GMSPlaceField.placeID.rawValue) |
                                                                  UInt(GMSPlaceField.coordinate.rawValue) |
                                                                  GMSPlaceField.addressComponents.rawValue |
                                                                  GMSPlaceField.formattedAddress.rawValue)
        autocompleteController.placeFields = fields

        let filter = GMSAutocompleteFilter()
        filter.type = .address
        autocompleteController.autocompleteFilter = filter
        return autocompleteController
    }

    func updateUIViewController(_ uiViewController: GMSAutocompleteViewController, context: UIViewControllerRepresentableContext<PlacePicker>) {
    }

    class GooglePlacesCoordinator: NSObject, UINavigationControllerDelegate, GMSAutocompleteViewControllerDelegate {

        var parent: PlacePicker

        init(_ parent: PlacePicker) {
            self.parent = parent
        }

        func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
            DispatchQueue.main.async {
                print(place.description.description as Any)
                self.parent.address =  place.name!
                self.parent.presentationMode.wrappedValue.dismiss()
                print("latitude: \(place.coordinate.latitude)")
                print("longitude: \(place.coordinate.longitude)")
                self.parent.latitude=place.coordinate.latitude
                self.parent.longitude=place.coordinate.longitude
            }
        }

        func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
            print("Error: ", error.localizedDescription)
        }

        func wasCancelled(_ viewController: GMSAutocompleteViewController) {
            parent.presentationMode.wrappedValue.dismiss()
        }

    }
}

Any way to add a button/label with "use current location" at the top of this list? enter image description here

0

There are 0 best solutions below