gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:35:49 25 4
gpt4 key购买 nike

`你好☺️我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com