代码之家  ›  专栏  ›  技术社区  ›  sourceplaze

如何使用GMSPlacePickerViewController类?

  •  1
  • sourceplaze  · 技术社区  · 6 年前

    我创建了如下选择器:

    let placePicker = GSMPlacePickerViewController(config: config)
    
    placePicker.delegate?.placePicker(viewController: GSMPlacePickerViewController, didPick: GSMPlace)
    

    在上面代码的第二行中,如何将用户选择的位置存储在我自己的变量中,以便以后可以访问它的变量 properties 比如坐标?

    GSMPlacePickerViewController

    GSMPlacePickerViewController docs ,它说我不必实现第一个参数,如果我理解正确的话,我可以将它留空

    此方法的实现应将视图控制器视为 视图控制器不会自行关闭。

    我该如何实现这一目标?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Luke Stanyer    6 年前

    添加 对视图控制器执行以下操作:

    class YourViewController: GMSPlacePickerViewControllerDelegate {
    
    }
    

    然后在所述VC中添加delegate方法,当用户选择地点时将调用该方法。然后关闭并访问如下位置数据:

     func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
                    placePicker.dismiss(animated: true, completion: nil)
                    let location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
    }