I am integrating Google Places in my SwiftUI app. I have searched a lot but didn't get any proper result for SwiftUI. I tried the solution in the comments of place autocomplete but didn't get my desired work.
I want:
I tried the following solution which helped me a lot to get my desired work but there is an issue with this is that for every location I searched, I got that location but the latitude and longitude always comes -180 for all locations.
Here is my code for View:
import SwiftUI
import GooglePlaces
struct MyGooglePlace: View {
@State var openPlacePicker = false
@State var address = ""
var body: some View {
VStack {
Text(address)
Button {
openPlacePicker.toggle()
} label: {
Text("open place picker")
}
}.sheet(isPresented: $openPlacePicker) {
PlacePicker(address: $address)
}
}
}
And the PlacePicker is:
import Foundation
import UIKit
import SwiftUI
import GooglePlaces
struct PlacePicker: UIViewControllerRepresentable {
func makeCoordinator() -> GooglePlacesCoordinator {
GooglePlacesCoordinator(self)
}
@Environment(\.presentationMode) var presentationMode
@Binding var address: String
func makeUIViewController(context: UIViewControllerRepresentableContext<PlacePicker>) -> GMSAutocompleteViewController {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = context.coordinator
let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.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)")
}
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
parent.presentationMode.wrappedValue.dismiss()
}
}
}
If I searched for some place in USA, it gives the coordinates -180, -180, and if I search for some location in Pakistan, it still gives -180, -180 coordinates.
Does anyone knows how to get the exact coordinates of the searched location?