按完成按钮后如何添加标记?

时间:2017-09-10 03:11:36

标签: ios swift google-maps

好的,所以我想在点击完成按钮时添加标记。当我点击MapVC中的完成按钮时,我想将当前位置标记添加到AddPinPointVC类。我该怎么办呢?

以下是相关代码:

MapsVC

class MapsVC: UIViewController {

weak var googleMaps: GMSMapView!

var locationManager = CLLocationManager()
var currentLocation: CLLocation?
var placesClient: GMSPlacesClient!
var zoomLevel: Float = 15.0

override func viewDidLoad() {
    super.viewDidLoad()

let camera = GMSCameraPosition.camera(withLatitude: 39.9533, longitude: -75.1593, zoom: 15.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    mapView.settings.myLocationButton = true
    self.view = mapView

locationManager = CLLocationManager()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.distanceFilter = 50
    locationManager.startUpdatingLocation()
    locationManager.delegate = self as? CLLocationManagerDelegate
    placesClient = GMSPlacesClient.shared()

if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
        currentLocation = locationManager.location
    }
}

static var latitude: CLLocationDegrees?
}

AddPinPointVC

class AddPinPointVC: UIViewController, UICollectionViewDelegateFlowLayout,UIPickerViewDelegate, UIPickerViewDataSource {

var pickerdata: [String] = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(handleCancel))

//function for cancel button
func handleCancel() { dismiss(animated: true, completion: nil) }

func handleDone(){
    dismiss(animated: true, completion: nil)
    let userMarker = GMSMarker()
    userMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees, longitude: CLLocationDegrees)
//        userMarker.title = typeOfPlaces[row]
    userMarker.snippet = ""
 }
}

如果还有其他问题,请告诉我们!谢谢。

1 个答案:

答案 0 :(得分:1)

您应该使用在按下“完成”按钮时触发的功能为此设置委托

协定

protocol AddPinPointDelegate: class {
    func addPinPointWillDismiss(_ marker: GMSMarker)
}

<强> MapsVC

class MapsVC: UIViewController, AddPinPointDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: 39.9533, longitude: -75.1593, zoom: 15.0)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        [...]
    }

    [...]

    var mapView: GMSMapView!

    //assuming you open AddPinPointVC via a segue
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "presentAddPinPoint" { //the identifier you gave for the segue
            (segue.destination as! AddPinPointVC).delegate = self
        }
    }

    func addPinPointWillDismiss(_ marker: GMSMarker) {
        marker.map = mapView
    }
}

<强> AddPinPointVC

class AddPinPointVC: UIViewController, UICollectionViewDelegateFlowLayout, UIPickerViewDelegate, UIPickerViewDataSource {
    [...]

    weak var delegate: AddPinPointDelegate?

    func handleDone() {
        let userMarker = GMSMarker()
        userMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees, longitude: CLLocationDegrees)
        //userMarker.title = typeOfPlaces[row]
        userMarker.snippet = ""
        delegate?.addPinPointWillDismiss(userMarker)

        dismiss(animated: true)
    }
}