ios - Swift 3.0 在 MapView 上放置注释

标签 ios swift annotations mkmapview

我设置了我的代码,以便在 mapView 上长按将允许用户放下图钉。我想知道是否有一种方法可以让用户输入注释的标题,这样在按下注释后(一旦放置了图钉),标题就会显示出来。

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    let mapPress = UILongPressGestureRecognizer(target: self, action: #selector(AddressVC.addAnnotation(_:)))
    mapPress.minimumPressDuration = 1.5
    mapView.addGestureRecognizer(mapPress)
    }

func addAnnotation(_ recognizer: UIGestureRecognizer){
    let annotations = self.mapView.annotations
    self.mapView.removeAnnotations(annotations)
    let touchedAt = recognizer.location(in: self.mapView) // adds the location on the view it was pressed
    let newCoordinates : CLLocationCoordinate2D = mapView.convert(touchedAt, toCoordinateFrom: self.mapView) // will get coordinates

    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinates
    self.mapView.addAnnotation(annotation)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        return nil
    }
    let identifier = "pinAnnotation"
    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
        annotationView.annotation = annotation
        return annotationView
    } else {
        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier: identifier)
        annotationView.isEnabled = true
        annotationView.canShowCallout = true

        return annotationView
    }
}

最佳答案

我建议使用带有 UITextFieldUIAlertController 供用户输入名称。

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let tap = UILongPressGestureRecognizer(target: self, action: #selector(self.createAnnotation))

        self.map.addGestureRecognizer(tap)
    }

    func createAnnotation(gesture: UILongPressGestureRecognizer){

        if gesture.state == .began {

            let point = gesture.location(in: self.map)

            let coordinate = self.map.convert(point, toCoordinateFrom: nil)

            let alertVC = UIAlertController(title: "Create Pin", message: "Enter a title for your pin.", preferredStyle: .alert)

            alertVC.addTextField { (textfield) in

                textfield.placeholder = "Enter a Title"

                textfield.autocapitalizationType = .words
            }

            alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

            alertVC.addAction(UIAlertAction(title: "Create", style: .default, handler: { (action) in

                guard

                    let textfield = alertVC.textFields?.first,

                    let text = textfield.text

                    else {

                        return
                }

                let pin = MKPointAnnotation()

                pin.title = text

                pin.coordinate = coordinate

                self.map.addAnnotation(pin)
            }))

            self.present(alertVC, animated: true, completion: nil)
        }
    }
}

Screenshot of UIAlertController

关于ios - Swift 3.0 在 MapView 上放置注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44359054/

相关文章:

java - 如何使用单一表单将属性设置为 2 个域对象,这里 1 个域对象充当 formbacking 对象

像IOS一样的安卓年历

ios - 在 Phonegap 生成的应用商店中部署 .ipa 文件?

swift - c-style for 语句被扭曲弃用

ios - 从另一个 viewController 快速检测 - iOS swift

orm - 如何在 Doctrine 2 中映射单个字符列

java - Hibernate Spring 注解混淆

android - 如何将图像转换为base64以使用http帖子请求发送

android - React Native 链接外部应用程序

macos - Swift 中的动态变量名称