gpt4 book ai didi

swiftui - 带有 SwiftUI 的 Google 地方自动完成 API

转载 作者:行者123 更新时间:2023-12-03 17:13:03 25 4
gpt4 key购买 nike

我正在尝试使用 SwiftUI 在我的应用程序中实现自动搜索地址。谷歌教程基于 UIKit,我希望如果有人尝试过并且可以指导我走向正确的方向。谢谢

最佳答案

试试这个代码;这个对我有用。

import Foundation
import UIKit
import SwiftUI
import GooglePlaces


struct PlacePicker: UIViewControllerRepresentable {

func makeCoordinator() -> Coordinator {
Coordinator(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
filter.country = "ZA"
autocompleteController.autocompleteFilter = filter
return autocompleteController
}

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

class Coordinator: 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()
}
}

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

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

}
}

关于swiftui - 带有 SwiftUI 的 Google 地方自动完成 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58503371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com