How to return gms location name from lat long swiftui

97 views Asked by At

I have a place picker that gives me the location by searching the street name. How I can retrieve the location name with this same class Placepicker just giving the lat long coordinates?

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("xxxx")

        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.formattedAddress ?? "adresa gresita"
                self.parent.presentationMode.wrappedValue.dismiss()
                
                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()
        }

    }
}

How can I add a function like, giveLocationByLatLong(lat: Double, long: Double)? Or can I use some stuff from the place picker class?

1

There are 1 answers

0
dhaval123 On BEST ANSWER

Pass the lat long with the variables:

    @Binding var latitude: Double
    @Binding var longitude: Double