2017-03-29 128 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

檢查並確定是否有任何錯誤或地圖未滾動。 –