【问题标题】:How to create a GMSPlace如何创建 GMSPlace
【发布时间】:2016-02-18 22:51:29
【问题描述】:

我正在使用Google Maps iOS SDK 并希望存储用户搜索的最近地点(GMSPlace 对象)。我在检索这些地方时遇到了问题,因为 GMSPlaces 似乎不能直接实例化。

是否可以实例化GMSPlace 对象?

谢谢!

【问题讨论】:

  • 希望有人能回答这个问题……你有没有想过?
  • @JohnFarkerson 我没有。不过,我不相信这是可能的。
  • 我在下面添加了答案。看看它是否适合你,不要忘记投票。谢谢:D

标签: swift google-maps google-maps-sdk-ios


【解决方案1】:

创建 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!让我知道它是否适合你。

【讨论】:

    【解决方案2】:

    不,您不能实例化 GMSPlace 对象。

    【讨论】:

    • 是的,你可以,在下面查看我的答案 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多