创建 GMS 位置的实例
是的,您可以实例化 GMS 地点对象。我在网上查的那天,我遇到了你的问题,希望能找到答案,但我没有找到任何答案。
所以,我想出了自己的解决方案。以下是创建 GMSPlace 实例的方法。
因此,我的要求是将 3 个 GMS Places 存储在 3 个不同的 GMSPlace 变量中。这就是我所做的:
var currentLocation: GMSPlace?
var selectedFromDestination: GMSPlace?
var selectedToDestination: GMSPlace?
现在,currentLocation 将存储用户的当前位置, selectedFromDestination 将存储起始地址点,而 selectedToDestination 将存储目标地址点。
注意:请注意,它们都是可选的。这是我唯一让它工作的,因为我仍然是 Swift 的菜鸟。
//MARK:- FUNCTION FOR GETTING CURRENT LOCATION
func getCurrentPlace() {
placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let placeLikelihoodList = placeLikelihoodList {
let place = placeLikelihoodList.likelihoods.first?.place
if let place = place {
self.nameLabel.text = place.name + "\(place.coordinate.latitude) , \(place.coordinate.longitude)"
self.addressLabel.text = place.formattedAddress?.components(separatedBy: ", ")
.joined(separator: "\n")
//assigning returned GMSPlace place object to currentlocation
self.currentLocation = place
}
}
})
}
现在,在我的viewDidLoad() 方法中,我调用了这个函数getCurrentPlace(),然后它将获取用户的当前位置并存储在currentLocation 变量中。
现在,您可以在 ViewController 中的任何位置访问此变量,并访问其属性,例如
currentLocation?.formattedAddress
currentLocation?.coordinate.latitude
currentLocation?.coordinate.longitude
等等。
现在,如果您想将用户选择的内容存储为取货地址和目的地地址,请继续阅读以下内容:-)
好的,所以现在您有两个文本字段,您需要在其上打开 GSMAutoCompleteViewController(附注 - 还有其他方法,但我更喜欢使用它,因为它很容易实现。)
因此,您将需要一个 textField Delegate 方法,该方法将根据我的 strTextFieldType 变量区分两个 textField 并显示 ViewController。
//MARK:- TEXTFIELD DELEGATE
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == txtFromDestination
{
strTxtFieldType = "FromDestination"
}
else if textField == txtToDestination
{
strTxtFieldType = "ToDestination"
}
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.present(acController, animated: true, completion: nil)
return false
}
我已将 return 设置为 false,因为我不希望 textFields 可编辑。
以下是您需要为 ViewController 实现的扩展代码
extension YOUR_VIEWCONTROLLER_NAME: GMSAutocompleteViewControllerDelegate {
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress ?? "null")")
if strTxtFieldType == "FromDestination"
{
self.txtFromDestination.text = place.formattedAddress
self.selectedFromDestination = place
}
else
{
self.txtToDestination.text = place.formattedAddress
self.selectedToDestination = place
}
print("Place attributions: \(String(describing: place.attributions))")
self.dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
// print("Error: \(error.description)")
self.dismiss(animated: true, completion: nil)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
print("Autocomplete was cancelled.")
self.dismiss(animated: true, completion: nil)
}
}
好了,你现在完成了。您可以在任何地方访问存储在 selectedFromDestination 和 selectedToDestination 中的值,就像 currentLocation GMSPlace 对象一样。
希望这对您有所帮助; :-D!让我知道它是否适合你。