首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建GMSPlace

如何创建GMSPlace
EN

Stack Overflow用户
提问于 2016-02-19 06:51:30
回答 2查看 1.8K关注 0票数 9

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

可以实例化GMSPlace对象吗?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2018-04-09 14:09:30

创建GMS PLACE的实例

是的,你可以实例化一个GMS Place对象。有一天我上网查了查,我碰到了你的问题,希望能找到答案,但我什么也没找到。

因此,我提出了自己的解决方案。下面是如何创建GMSPlace实例的方法。

因此,我的要求是在3个不同的GMSPlace变量中存储3个GMS位置。下面是我所做的:

代码语言:javascript
复制
var currentLocation: GMSPlace?
var selectedFromDestination: GMSPlace?
var selectedToDestination: GMSPlace?

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

注意:请注意,它们都是可选的。这是我让它工作的唯一方法,因为我仍然是Swift的新手。

代码语言:javascript
复制
 //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中的任何位置访问此变量,并访问其属性,例如

代码语言:javascript
复制
 currentLocation?.formattedAddress
 currentLocation?.coordinate.latitude
 currentLocation?.coordinate.longitude

诸若此类。

现在,如果您想存储用户选择作为收件地址和目的地地址的内容,请阅读下面的内容:-)

好了,现在你有两个文本字段,你需要在上面打开GSMAutoCompleteViewController(P.S -还有其他方法,但我更喜欢使用它,因为它很容易实现。)

因此,您将需要一个textField委托方法,该方法将根据我的strTextFieldType变量区分两个textField,并显示ViewController。

代码语言:javascript
复制
//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实现的扩展的代码

代码语言:javascript
复制
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 anywhere中的值。

希望这能有所帮助;:-D!让我知道它对你是否有效。

票数 2
EN

Stack Overflow用户

发布于 2017-06-03 22:46:02

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35494076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档