谷歌选择iOS的位置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌选择iOS的位置相关的知识,希望对你有一定的参考价值。

我读了this并尝试构建一个位置选择器应用程序,其中下面的代码完美地工作:

import UIKit
import GooglePlacePicker

class ViewController: UIViewController {

  // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
  @IBOutlet var nameLabel: UILabel!
  @IBOutlet var addressLabel: UILabel!

  // Add a UIButton in Interface Builder, and connect the action to this function.
  @IBAction func pickPlace(_ sender: UIButton) {
    let center = CLLocationCoordinate2D(latitude: 37.788204, longitude: -122.411937)
    let northEast = CLLocationCoordinate2D(latitude: center.latitude + 0.001, longitude: center.longitude + 0.001)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - 0.001, longitude: center.longitude - 0.001)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)
    let placePicker = GMSPlacePicker(config: config)

    placePicker.pickPlace(callback: {(place, error) -> Void in
      if let error = error {
        print("Pick Place error: (error.localizedDescription)")
        return
      }

      if let place = place {
        self.nameLabel.text = place.name
        self.addressLabel.text = place.formattedAddress?.components(separatedBy: ", ")
          .joined(separator: "
")
      } else {
        self.nameLabel.text = "No place selected"
        self.addressLabel.text = ""
      }
    })
  }
}

显然GMSPlacePicker已被弃用,并被GMSPlacePickerViewController取代,所以我尝试了here示例:

import UIKit
import GooglePlacePicker

class ViewController: UIViewController {

  // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
  @IBOutlet var nameLabel: UILabel!
  @IBOutlet var addressLabel: UILabel!
// The code snippet below shows how to create and display a GMSPlacePickerViewController.
@IBAction func pickPlace(_ sender: UIButton) {
  let config = GMSPlacePickerConfig(viewport: nil)
  let placePicker = GMSPlacePickerViewController(config: config)

  present(placePicker, animated: true, completion: nil)
}

// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
  // Dismiss the place picker, as it cannot dismiss itself.
  viewController.dismiss(animated: true, completion: nil)

  print("Place name (place.name)")
  print("Place address (place.formattedAddress)")
  print("Place attributions (place.attributions)")
}

func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
  // Dismiss the place picker, as it cannot dismiss itself.
  viewController.dismiss(animated: true, completion: nil)

  print("No place selected")
}
}

但它运作不正常,我在这里遗漏了什么吗?弹出位置选择器,但取消按钮既不可点击,也不会在选择所需位置时关闭位置选择器,因此不会打印任何内容!

答案

感谢@tassinai在完整工作代码下面的评论:

import UIKit
import GooglePlacePicker

class ViewController: UIViewController, GMSPlacePickerViewControllerDelegate  {
    @IBOutlet weak var placeNameLabel: UILabel!

    @IBAction func placePicker(_ sender: Any) {
        let config = GMSPlacePickerConfig(viewport: nil)
        let placePicker = GMSPlacePickerViewController(config: config)

        placePicker.delegate = self

        present(placePicker, animated: true, completion: nil)
    }

    func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
        viewController.dismiss(animated: true, completion: nil)
        print("Place name (place.name)")
        placeNameLabel.text = place.name
    }

    func placePicker(_ viewController: GMSPlacePickerViewController, didFailWithError error: Error) {
    // In your own app you should handle this better, but for the demo we are just going to log
    // a message.
         NSLog("An error occurred while picking a place: (error)")
    }

    func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
        // Dismiss the place picker, as it cannot dismiss itself.
        viewController.dismiss(animated: true, completion: nil)

        print("No place selected")
        placeNameLabel.text = "No place selected"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
  }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

下面是另一种编写上面代码的方法:

import UIKit
import GooglePlacePicker

class ViewController: UIViewController {
    @IBOutlet weak var placeNameLabel: UILabel!

    @IBAction func placePicker(_ sender: Any) {
        let config = GMSPlacePickerConfig(viewport: nil)
        let placePicker = GMSPlacePickerViewController(config: config)

        placePicker.delegate = self

        present(placePicker, animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
  }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController : GMSPlacePickerViewControllerDelegate {
    func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
        viewController.dismiss(animated: true, completion: nil)
        print("Place name (place.name)")
        placeNameLabel.text = place.name
    }

    func placePicker(_ viewController: GMSPlacePickerViewController, didFailWithError error: Error) {
        // In your own app you should handle this better, but for the demo we are just going to log
        // a message.
        NSLog("An error occurred while picking a place: (error)")
    }

    func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
        // Dismiss the place picker, as it cannot dismiss itself.
        viewController.dismiss(animated: true, completion: nil)

        print("No place selected")
        placeNameLabel.text = "No place selected"
    }
}

以上是关于谷歌选择iOS的位置的主要内容,如果未能解决你的问题,请参考以下文章

谷歌选择iOS的位置

谷歌丰富的片段不工作

如何在Android的谷歌地图片段中更改默认的蓝色圆形位置图标?

android - 离开应用程序时保持谷歌地图片段在位置上放大

如何更改谷歌地图标记上方的标题和片段设计

谷歌地图 V2 位置返回 null