2017-03-29 122 views
2

我在我的应用程序中使用iOS版Google Maps SDK。我需要在地图移动时获得地址。但是idleAtCameraPosition未被触发。GMSMapViewDelegate idleAtCameraPosition没有被触发

func vwGMap(vwGMap: GMSMapView!, idleAtCameraPosition position: GMSCameraPosition!) { 
     print("changed position: \(vwGMap.camera.target)") 
} 

我在viewDidLoad()

vwGMap.delegate = self 

添加委托,宣布地图视图

@IBOutlet var vwGMap: GMSMapView! 

为什么idleAtCameraPosition不触发?

完整代码

class MapViewController: BaseViewController,CLLocationManagerDelegate, UISearchBarDelegate ,GMSMapViewDelegate, LocateOnTheMap,GMSAutocompleteFetcherDelegate { 

    @IBOutlet var marker: UIImageView! 

    @IBOutlet var fromaddress: kTextFiledPlaceHolder! 

    @IBOutlet var toaddress: kTextFiledPlaceHolder! 
    @IBOutlet var fromdummy: kTextFiledPlaceHolder! 

    @IBOutlet var vwGMap: GMSMapView! 
    var searchResultController: SearchResultsController! 
    var resultsArray = [String]() 
    var gmsFetcher: GMSAutocompleteFetcher! 


    var locationManager = CLLocationManager() 
    var didFindMyLocation = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     self.addSlideMenuButton() 



     //Location Manager code to fetch current location 
     self.locationManager.delegate = self 
     self.locationManager.requestWhenInUseAuthorization() 

     self.locationManager.startUpdatingLocation() 



     vwGMap.delegate = self 



     } 
    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(true) 



     searchResultController = SearchResultsController() 
     searchResultController.delegate = self 
     gmsFetcher = GMSAutocompleteFetcher() 
     gmsFetcher.delegate = self 

    } 







    //Location Manager delegates 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
      let location = locations.last 

       let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:16) 
     vwGMap.animate(to: camera) 

     CATransaction.begin() 
     CATransaction.setValue(5, forKey: kCATransactionAnimationDuration) 

     vwGMap.animate(toViewingAngle: 40) 
     vwGMap.animate(toBearing: 80) 

     //vwGMap.animate(toZoom: 18) 

      CATransaction.commit() 
     vwGMap.settings.myLocationButton = true 
     vwGMap.isMyLocationEnabled = true 

     vwGMap.delegate = self 
     self.vwGMap.addSubview(toaddress) 
     self.vwGMap.addSubview(fromdummy) 
     self.vwGMap.addSubview(marker) 


     self.locationManager.stopUpdatingLocation() 

    } 





    public func didFailAutocompleteWithError(_ error: Error) { 
     //  resultText?.text = error.localizedDescription 
    } 

    /** 
    * Called when autocomplete predictions are available. 
    * @param predictions an array of GMSAutocompletePrediction objects. 
    */ 
    public func didAutocomplete(with predictions: [GMSAutocompletePrediction]) { 
     //self.resultsArray.count + 1 
     print(predictions) 
     for prediction in predictions { 

      if let prediction = prediction as GMSAutocompletePrediction!{ 
       self.resultsArray.append(prediction.attributedFullText.string) 
      } 
     } 
     self.searchResultController.reloadDataWithArray(self.resultsArray) 
     // self.searchResultsTable.reloadDataWithArray(self.resultsArray) 
     print("check") 
     print(resultsArray) 
    } 

    func locateWithLongitude(_ lon: Double, andLatitude lat: Double, andTitle title: String) { 



     DispatchQueue.main.async {() -> Void in 


      let position = CLLocationCoordinate2DMake(lat, lon) 
      let marker = GMSMarker(position: position) 

      let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 10) 
//   self.vwGMap.camera = camera 
//    
//   marker.title = "Address : \(title)" 
//   marker.map = self.vwGMap 
      CATransaction.begin() 
      CATransaction.setValue(5, forKey: kCATransactionAnimationDuration) 
      self.vwGMap.animate(to: camera) 

      CATransaction.commit() 

     } 

    } 
    func locate(_ lon: Double, andLatitude lat: Double) { 



     DispatchQueue.main.async {() -> Void in 



      let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 10) 
      //   self.vwGMap.camera = camera 
      // 
      //   marker.title = "Address : \(title)" 
      //   marker.map = self.vwGMap 
      CATransaction.begin() 
      CATransaction.setValue(5, forKey: kCATransactionAnimationDuration) 
      self.vwGMap.animate(to: camera) 

      CATransaction.commit() 

     } 

    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 

     //  let placeClient = GMSPlacesClient() 
     // 
     // 
     //  placeClient.autocompleteQuery(searchText, bounds: nil, filter: nil) {(results, error: Error?) -> Void in 
     //   // NSError myerr = Error; 
     //   print("Error @%",Error.self) 
     // 
     //   self.resultsArray.removeAll() 
     //   if results == nil { 
     //    return 
     //   } 
     // 
     //   for result in results! { 
     //    if let result = result as? GMSAutocompletePrediction { 
     //     self.resultsArray.append(result.attributedFullText.string) 
     //    } 
     //   } 
     // 
     //   self.searchResultController.reloadDataWithArray(self.resultsArray) 
     // 
     //  } 

     print(searchText) 

     self.resultsArray.removeAll() 
     gmsFetcher?.sourceTextHasChanged(searchText) 


    } 

    func mapView(_ mapView: GMSMapView, idleAtCamera position: GMSCameraPosition) { 
     print("changed position") 
    } 

} 
+0

检查更新我的答案。 –

回答

1

确保类扩展到GMSMapViewDelegate。

示例代码:

class Class-name : UIViewController,GMSMapViewDelegate 

变化代码viewDidLoad中:

vwGMap = GMSMapView() 
vwGMap.delegate = self 

使用此方法用于自来水标志事件。

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { 


print("You tapped at \(coordinate.latitude), \(coordinate.longitude)") 
} 

如果地图视图是滚动:

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition) { 
    print("map scrolled.") 
} 
+0

是的,我已经扩展像那样。是否有任何地图视图声明或添加委派添加有问题? –

+0

现在检查调用了委托。 –

+0

检查并确定是否有任何错误或地图未滚动。 –