调用功能时无法更新我的GMsmapView

时间:2019-01-15 12:44:05

标签: swift

代码

  import UIKit
  import GooglePlaces
  import GoogleMaps

  class FirstViewController: UIViewController {


@IBOutlet weak var mapsView: GMSMapView!


var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var pick:UISearchController!
var drop:UISearchController!
var marker:GMSMarker!


var cgrectfordrop:CGRect?
var resultView: UITextView?


var gmspick:GMSAutocompleteResultsViewController!
var gmsdrop:GMSAutocompleteResultsViewController!
override func viewDidLoad() {

    super.viewDidLoad()

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    self.mapsView=mapView




    resultsViewController = GMSAutocompleteResultsViewController()
    resultsViewController?.delegate = self





    gmspick=GMSAutocompleteResultsViewController()
    gmspick.delegate=self
    gmspick.accessibilityLabel="picku"
    pick=UISearchController(searchResultsController: gmspick)
    pick.searchResultsUpdater = gmspick
    pick.searchBar.sizeToFit()
    pick.searchBar.accessibilityIdentifier="picku"
    pick.searchBar.placeholder="Enter pick up location"
    pick.hidesNavigationBarDuringPresentation = false
    pick.searchBar.sizeToFit()






    gmsdrop=GMSAutocompleteResultsViewController()
    gmsdrop.delegate=self
    gmsdrop.accessibilityLabel="dropu"
    drop=UISearchController(searchResultsController: gmsdrop)
    drop.searchResultsUpdater = gmsdrop
    drop.searchBar.placeholder="Enter drop location"
    drop.searchBar.sizeToFit()
    drop.hidesNavigationBarDuringPresentation = false
    drop.searchBar.sizeToFit()







   let pickyvalue=view.frame.height/2- 
    pick.searchBar.frame.height/2
       // cgrectfordrop=CGRect(x: 0, y: pickyvalue/3.9, width: 
   view.frame.width, height: 45.0)
        let subViewpick = UIView(frame: CGRect(x: 0, y: pickyvalue/9, 
    width: view.frame.width, height: 45.0))
        subViewpick.addSubview(pick.searchBar)
        view.addSubview(subViewpick)




     let dropyvalue=view.frame.height/2-drop.searchBar.frame.height/2
    //  cgrectfordrop=CGRect(x: 0, y: dropyvalue/11, width: 
     view.frame.width, height: 45.0)
     let subViewpickd = UIView(frame: CGRect(x: 0, y: dropyvalue/3, 
     width: view.frame.width, height: 45.0))
      subViewpickd.addSubview(drop.searchBar)
    view.addSubview(subViewpickd)



  }
   func settingmarker(location:CLLocationCoordinate2D){


    self.mapsView.animate(toLocation: location)


   }
}













  extension FirstViewController: 
 GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: 
 GMSAutocompleteResultsViewController,
                       didAutocompleteWith place: GMSPlace) {
    if (resultsController.accessibilityLabel=="picku") {
        pick.isActive=false
        print("Place name: \(place.name)")
        print("Place address: \(place.formattedAddress)")
        print("Place attributions: \(place.attributions)")
        print("pickup")

     settingmarker(location: place.coordinate)

    }else if(resultsController.accessibilityLabel=="dropu"){
        drop.isActive=false
        print("Place name: \(place.name)")
        print("Place address: \(place.formattedAddress)")
        print("Place attributions: \(place.attributions)")
        print("dropu")

    }


}

func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                       didFailAutocompleteWithError error: Error){
    // TODO: handle the error.
    print("Error: ", error.localizedDescription)
}

// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

}

调用设置标记功能时无法更新我的Gmsmapview 调用设置标记器功能时无法abble更新我的Gmsmapview不能调用 调用设置标记功能时无法更新我的Gmsmapview 调用设置标记功能时无法更新我的Gmsmapview

1 个答案:

答案 0 :(得分:0)

替换此部分

let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.mapsView = mapView

使用

self.mapsView.camera = camera

分配地图会将其设为nil,就像您在出口时将其声明为weak