【问题标题】:What is wrong with my auto complete?我的自动完成有什么问题?
【发布时间】:2018-09-22 03:54:41
【问题描述】:

`你好☺️我是新来的 swift 。我正在研究 Google 地图和 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)
}

【问题讨论】:

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


    【解决方案1】:
    **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
        }
    
    }
    

    使用 locationStart 和 locationEnd 可以绘制折线

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多