更新MapView引脚位置

问题描述 投票:0回答:1

所以我有一个MapView,它向当前用户位置添加了一个图钉。另外,我有两个文本字段显示了引脚的坐标。我想添加两个功能:

  1. 当我拖放图钉时,我想更新文本字段内的坐标。

  2. 当我在文本字段中编辑坐标时,应将图钉从文本字段移至更新的坐标。

[这里是我的代码,我在其中处理MapView的所有内容。我对编码仍然很陌生,因此代码可能会令人困惑,对此表示抱歉。

class LocateVC: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var finishedLocateButton: UIButton!
    @IBOutlet weak var relocateButton: UIButton!

    var coordinates: [[Double]]!
    var latTitleLabel:[String]!
    var latValueLabel:[String]!
    var lngTitleLabel:[String]!
    var lngValueLabel: [String]!

    var isEdited = false
    var customCallout: UIView?

    //Important to track location
    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()

        view.layer.backgroundColor = Colors.grey.cgColor
        //button titles
        relocateButton.isHidden = true
        relocateButton.setTitle("Relocate", for: .normal)
        finishedLocateButton.setTitle("Continue", for: .normal)
        navigationItem.title = "Location"
        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.requestWhenInUseAuthorization()
        locationManager.delegate = self
        self.mapView.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

        if isEdited == false {
            if CLLocationManager.locationServicesEnabled() {
                addPin()
            }
        }

        // Latitude,Longitude
        coordinates = [
            [ProductData.shared.latitude!, ProductData.shared.longitude!],
        ]

    } //end of viewDidLoad

    override func viewWillAppear(_ animated: Bool) {
        super.dismiss(animated: animated)

    }


    public func removePin() {


    }

    func dropPinFor(placemark: MKPlacemark) {

         for annotation in mapView.annotations {
             if annotation.isKind(of: MKPointAnnotation.self) {
                // mapView.removeAnnotation(annotation) // removing the pins from the map
             }
         }

         let annotation = MKPointAnnotation()
         annotation.coordinate = placemark.coordinate
         mapView.addAnnotation(annotation)
    }

    //1
    public func addPin() {

        if isEdited == false {
            ProductData.shared.latitude = locationManager.location?.coordinate.latitude
            ProductData.shared.longitude = locationManager.location?.coordinate.longitude
        }

        self.mapView.delegate = self
        // adds an annotation to coordinates from productData
        let point = ProductAnnotation(coordinate: CLLocationCoordinate2D(latitude: ProductData.shared.latitude! , longitude: ProductData.shared.longitude!))

        self.mapView.addAnnotation(point)
        // 3
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: ProductData.shared.latitude!, longitude: ProductData.shared.longitude!), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        self.mapView.setRegion(region, animated: true)
    }

    public func editCoord() {
        performSegue(withIdentifier: "editCoord", sender: CustomCalloutView.self)
    }

    @IBAction func relocateButtonClicked(_ sender: Any) {
        addPin()
    }
    @IBAction func finishedLocateButtonClicked(_ sender: Any) {
        performSegue(withIdentifier: "finishedLocateSegue2", sender: self)
    }

    //4
    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        if view.isKind(of: CustomCalloutView.self ) || view.isKind(of: AnnotationView.self) || view.isKind(of: ProductAnnotation.self) {
            return
        } else {
            customCallout?.removeFromSuperview()
        }
    }
      //3
      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if view.annotation is MKUserLocation {
            return
        }
        //this creates the callout
        let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
        let calloutView =  views?[0] as! CustomCalloutView
        calloutView.delegate = self
        calloutView.lngTitleLabel.text = "Lng"
        calloutView.latTitleLabel.text = "Lat"
        calloutView.lngTextField.text = String(format:"%f", ProductData.shared.longitude!)
        calloutView.latTextField.text = String(format:"%f", ProductData.shared.latitude!)
        calloutView.latTextField.layer.borderWidth = 0.0
        calloutView.lngTextField.layer.borderWidth = 0.0
        calloutView.latTextField.isEnabled = false
        calloutView.lngTextField.isEnabled = false
        calloutView.latTextField.keyboardType = .numberPad
        calloutView.lngTextField.keyboardType = .numberPad
        calloutView.alpha = 1.0
        calloutView.layer.cornerRadius = 8
        calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
        customCallout = calloutView
        view.addSubview(calloutView)
        mapView.setCenter((view.annotation?.coordinate)!, animated: true)
      }


    //2
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation is MKUserLocation {
            return nil
        }
        var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
        if annotationView == nil{
            annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
            annotationView?.isDraggable = true
            annotationView?.canShowCallout = false
        } else {
            annotationView?.annotation = annotation
        }

        annotationView?.image = UIImage(named: "dot")
        return annotationView
    }

    func saveButtonTapped() {
        customCallout?.removeFromSuperview()
    }
}


extension LocateVC: CustomCalloutViewDelegate {
    func didClickEditButton() {
        editCoord()
    }
    func didClickSaveButton() {
        saveButtonTapped()
    }
}

这里是我的自定义标注:

class CustomCalloutView: UIView {

    @IBOutlet weak var latTitleLabel: UILabel!
    @IBOutlet weak var lngTitleLabel: UILabel!
    @IBOutlet weak var editButton: UIButton!
    @IBOutlet weak var saveButton: UIButton!
    @IBOutlet weak var latTextField: UITextField!
    @IBOutlet weak var lngTextField: UITextField!

    var delegate: CustomCalloutViewDelegate?
    var isEditing: Bool = false

    @IBAction func didClickEditButton(_ sender: Any) {
//        delegate?.didClickEditButton()


        isEditing = true


        latTextField.layer.borderWidth = 1.0
        lngTextField.layer.borderWidth = 1.0
        latTextField.isEnabled = true
        lngTextField.isEnabled = true
        saveButton.isHidden = false
    }
    @IBAction func saveButtonTapped(_ sender: Any) {

        if isEditing == true {
            if let lat = latTextField.text {
                ProductData.shared.latitude = Double(lat)
            }
            if let lng = lngTextField.text {
                ProductData.shared.longitude = Double(lng)
            }

            latTextField.layer.borderWidth = 0.0
            lngTextField.layer.borderWidth = 0.0
            latTextField.isEnabled = false
            lngTextField.isEnabled = false
            self.saveButton.setTitle("Save", for: .normal)
            isEditing = false
        } else {
            self.saveButton.setTitle("Cancel", for: .normal)
            delegate?.didClickSaveButton()
        }
    }
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if self.bounds.contains(point) {
            return true
        } else {
            return self.subviews.contains { $0.frame.contains(point) }
        }
    }
}

protocol CustomCalloutViewDelegate {
    func didClickEditButton()
    func didClickSaveButton()
}

任何想法我怎么能做到这一点?也许以一般方式。

提前谢谢您

ios swift mkmapview
1个回答
0
投票

更改标记位置后获得坐标,例如

let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude

和设置坐标

let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)

如果您不保留标记参考,则>>

guard let marker = self.appleMap.annotations.first else {return}
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude

和设置坐标

guard let marker = self.appleMap.annotations.first else {return}
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.