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

时间:2017-07-25 21:57:10

标签: swift google-maps swift3 autocomplete

我在从谷歌地图检索数据时遇到问题放置自动填充因为我的视图确实加载了纬度和经度[Unity] sare默认显示地图

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!

2 个答案:

答案 0 :(得分:0)

您是否在父视图控制器中实现了GMSAutocompleteViewControllerDelegate协议?

    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
    }

}

答案 1 :(得分:0)

您缺少代理GMSAutocompleteViewControllerDelegate 需要添加为 -

class ProductDetailsViewController: UIViewController,GMSAutocompleteViewControllerDelegate {}

因此在此之后不再有力量包裹了

autocompleteController.delegate = self as! GMSAutocompleteViewControllerDelegate

写一下

autocompleteController.delegate = self

之后,您将获得信息

  func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
        print("latitude is ",place.coordinate.latitude)
        print("longitude is ",place.coordinate.longitude)

  }