如何创建GMSPlace

时间:2016-02-18 22:51:30

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

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

是否可以实例化GMSPlace对象?

谢谢!

2 个答案:

答案 0 :(得分:1)

不,您无法实例化GMSPlace对象。

答案 1 :(得分:0)

创建GMS PLACE实例

是的,您可以实例化GMS Place对象。我在互联网上查询的那一天,我遇到了你的问题希望找到答案,但我没有找到任何答案。

所以,我提出了自己的解决方案。这就是你如何创建一个GMSPlace实例。

所以,我的要求是在3个不同的GMSPlace变量中存储3个GMS位置。这就是我所做的:

var currentLocation: GMSPlace?
var selectedFromDestination: GMSPlace?
var selectedToDestination: GMSPlace?

现在,currentLocation将存储用户的当前位置,selectedFromDestination将存储起始地址点,而selectedToDestination将存储目标地址点。

注意:请注意,它们都是可选项。这是我唯一能让它发挥作用的,因为我仍然是Swob的Noob。

 //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(P.S - 还有其他方法,但我更喜欢使用它,因为它很容易实现。)

因此,您将需要一个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
}

我已将返回值设置为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)
}
}

在那里,你现在已经完成了。您可以像currentLocation GMSPlace对象一样访问存储在selectedFromDestination和selectedToDestination中的值。

希望这很有帮助; :-D! Lemme知道它是否适合你。

相关问题