gpt4 book ai didi

ios - 按下完成按钮时如何添加标记?

转载 作者:行者123 更新时间:2023-11-28 15:20:13 24 4
gpt4 key购买 nike

好的,所以我想在单击完成按钮时添加一个标记。当我单击 AddPinPointVC 中的完成按钮时,我想将当前位置标记添加到 MapVC 类。我需要做什么才能做到这一点?

相关代码如下:

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 = ""
}
}

如果有任何问题,请告诉我!谢谢。

最佳答案

您应该为此创建一个委托(delegate),并在按下“完成”按钮时触发一个函数。

协议(protocol)

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)
}
}

关于ios - 按下完成按钮时如何添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com