swift - 我的自动完成功能出了什么问题?

标签 swift google-maps autocomplete google-places-api

`你好☺️我是 swift 新手。我正在研究 Google map 和 Goole Place 。 我有一个搜索栏,当我单击按钮并搜索时,结果应该显示在自动完成位置,但这不起作用。 这是我的代码:

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
    self.googleMapsView.camera = camera
    self.dismiss(animated: true, completion: nil) //dismiss after select place
}

func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print("Erroe Auto Complte \(error)")
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    self.dismiss(animated: true, completion: nil)//when cancel search

}

@IBAction func openSearchAddress(_ sender: UIBarButtonItem) {
    let autoCompleteController = GMSAutocompleteViewController()
    autoCompleteController.delegate = self
    self.locationManager.startUpdatingLocation()
    self.present(autoCompleteController, animated: true, completion: nil)
}

最佳答案

**Declaration of variable**  
var locationManager = CLLocationManager()
var locationSelected = Location.startLocation
var locationStart = CLLocation()
var locationEnd = CLLocation()


**GMSMapViewDelegate Methods**

    func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
        viewMap.isMyLocationEnabled = true
    }

    func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
        viewMap.isMyLocationEnabled = true
        if (gesture) {
            mapView.selectedMarker = nil
        }
    }

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        viewMap.isMyLocationEnabled = true
        return false
    }

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        print("COORDINATE \(coordinate)") // when you tapped coordinate
    }

    func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
        viewMap.isMyLocationEnabled = true
        viewMap.selectedMarker = nil
        return false
    }

 **StartLocation and DetinationLocation**
@IBAction func btnStartLocationPressed(_ sender: UIButton) {
        let autoCompleteController = GMSAutocompleteViewController()
        autoCompleteController.delegate = self
        // selected location
        locationSelected = .startLocation
        self.locationManager.stopUpdatingLocation()
        self.present(autoCompleteController, animated: true, completion: nil)
    }
    @IBAction func btnDetinationLocation(_ sender: UIButton) {
        let autoCompleteController = GMSAutocompleteViewController()
        autoCompleteController.delegate = self
        // selected location
        locationSelected = .destinationLocation
        self.locationManager.stopUpdatingLocation()
        self.present(autoCompleteController, animated: true, completion: nil)
    }

**This is Extension for GMSAutocompleteViewController put after ViewController class** 


extension ViewController: GMSAutocompleteViewControllerDelegate {

    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
        print("Error \(error.localizedDescription)")
    }

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

        // Change map location
        let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 16.0
        )
        // set coordinate to text
        if locationSelected == .startLocation {
            btnStart.setTitle("\(place.coordinate.latitude), \(place.coordinate.longitude)", for: .normal) 
            locationStart = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        } else {
            btndesti.setTitle("\(place.coordinate.latitude), \(place.coordinate.longitude)", for: .normal)
            locationEnd = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        }
        self.viewMap.camera = camera
        self.dismiss(animated: true, completion: nil)
    }

    func wasCancelled(_ viewController: GMSAutocompleteViewController) {
        self.dismiss(animated: true, completion: nil)
    }

    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

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

}

using locationStart and locationEnd you can draw polyline

关于swift - 我的自动完成功能出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789034/

相关文章:

ios - 在 iOS 后台进行信标测距

ios - 如何从 UIImagePicker 推送到 UIViewController

android - 使用 Google Play 服务中的设置 API 启用 "High Accuracy"位置

android - 模块根文件夹中缺少文件 google-services.json(但它在好地方!)

android - 如何定位城市或地区?

php - Jquery UI 自动完成功能在 IE10 上不起作用

c++ - 键入循环时如何修复 visual studio 自动完成?

ios - 如何减去日期成分?

swift - 更改 AKPlayer 文件时 Audiokit 崩溃

php - 数据库自动完成功能不起作用