swift - 我如何通过搜索地点来检索坐标

标签 swift google-maps swift3 autocomplete

我在从谷歌地图地点自动完成中检索数据时遇到了问题,因为我的 View 在显示 map 时确实加载了纬度和经度 [Unity] 是默认值

let camera = GMSCameraPosition.camera(withLatitude: 30.7096, longitude: 76.7016, zoom: 12)

let mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 58, width: 320, height: 510), camera: camera)

    let marker = GMSMarker()
    marker.position = camera.target
    marker.snippet = "Hello World"
    marker.map = mapView
    self.mapSize.addSubview(mapView)
}

@IBAction func autocompleteClicked(_ sender: UIButton)
{
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self as! GMSAutocompleteViewControllerDelegate
    present(autocompleteController, animated: true, completion: nil)
}

@IBOutlet weak var mapSize: UIView!

最佳答案

您是否在父 View Controller 中实现了 GMSAutocompleteViewControllerDelegate 协议(protocol)?

    extension ViewController: GMSAutocompleteViewControllerDelegate {

    // Handle the user's selection.
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        print("Place name: \(place.name)")
        print("Place address: \(place.formattedAddress)")
        print("Place attributions: \(place.attributions)")
        dismiss(animated: true, completion: nil)
    }

    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
        // TODO: handle the error.
        print("Error: ", error.localizedDescription)
    }

    // User canceled the operation.
    func wasCancelled(_ viewController: GMSAutocompleteViewController) {
        dismiss(animated: true, completion: nil)
    }

    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

}

关于swift - 我如何通过搜索地点来检索坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45314182/

相关文章:

swift - updateChildValues 使用字典或 url

ios - UIPageViewController 空白屏幕

android - 谷歌地图 API v2 不工作

android - 如何在我的应用程序中使用 Google map 搜索功能 api?

ios - 重写 func prepare(for segue : UIStoryboardSegue, sender : Any?) 不能正常工作

ios - 无法在 Objective-C 中使用 Swift 生成的共享框架

swift - 语法,== 运算符,当不在 if 语句中时。

javascript - 如果在 resize 事件后调用 Google Maps API fitBounds() 行为会有所不同

ios - 从挂载 map 的数组中检索信息 (MapKit)

ios - swift ,相机图像不返回