好的,所以我想在点击完成按钮时添加标记。当我点击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 = ""
}
}
如果还有其他问题,请告诉我们!谢谢。
答案 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)
}
}