【问题标题】:MapKit functionality on a UIImage, dropping pins/AnnotationUIImage 上的 MapKit 功能,放置图钉/注释
【发布时间】:2021-11-09 19:31:29
【问题描述】:

我正在尝试在图像上使用现有的 MapKit 功能。 我想要实现的是在图像上放置图钉,并可能在这些图钉上添加注释。 我设法让用户可以使用 longGesture 动态添加图钉,但我不知道如何在图像上实现相同的效果。

我的代码如下:

import UIKit
import MapKit

class ViewController: UIViewController , MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!
    var keyLat:String = "49.2768"
    var keyLon:String = "-123.1120"
    @IBOutlet weak var image: UIImageView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        mapView.delegate = self
        
        let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        longPressRecogniser.minimumPressDuration = 0.5
        mapView.addGestureRecognizer(longPressRecogniser)
        
        mapView.mapType = MKMapType.standard
        
        let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(keyLat.toFloat()),longitude: CLLocationDegrees(keyLon.toFloat()))
        
        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        mapView.setRegion(region, animated: true)
        
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "MY Pin"
        annotation.subtitle = "On the Map"
        mapView.addAnnotation(annotation)
    }

    
    @objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
    {
        
        let location = gestureReconizer.location(in: mapView)
        let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
        
        // Add annotation:
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "latitude:" + String(format: "%.02f",annotation.coordinate.latitude) + "& longitude:" + String(format: "%.02f",annotation.coordinate.longitude)
        mapView.addAnnotation(annotation)
        
    }
    
    
    var selectedAnnotation: MKPointAnnotation?
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        
        let latValStr : String = String(format: "%.02f",Float((view.annotation?.coordinate.latitude)!))
        let lonvalStr : String = String(format: "%.02f",Float((view.annotation?.coordinate.longitude)!))
        
        print("latitude: \(latValStr) & longitude: \(lonvalStr)")
    }
    
}

任何帮助将不胜感激。 谢谢 乔治

【问题讨论】:

    标签: swift uiimageview mapkit mapkitannotation


    【解决方案1】:

    按照相同的程序并在此处和此处进行一些调整,可以实现相同的效果。我制作了一个示例ViewController,演示了如何将指针(在本例中为UIViews)添加到UIImageView

    class ViewController: UIViewController {
    
        @IBOutlet weak var imageView: UIImageView! // Image view
    
        lazy var longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressScreen)) // long press gesture
    
        // MARK: LifeCycle
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            setup()
        }
    
        // MARK: Functions
        private func setup() {
            imageView.image = UIImage(named: "photo")
    
            imageView.addGestureRecognizer(longPress) // Adding gesture recognizer 
            imageView.isUserInteractionEnabled = true // ImageViews are not user interactive by default
    
        }
    
        // UILongPressGestureRecognizer Action
        @objc func didLongPressScreen(_ sender: UILongPressGestureRecognizer) {
            let location = sender.location(in: self.view) //Getting location
    
            DispatchQueue.main.async {
                let pointer = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
                pointer.backgroundColor = .red
                pointer.center = location // Setting the center of the view to the x,y coordinates of the long press
                self.view.addSubview(pointer) // Adding the UIView to the view
            }
        }
    }
    

    其中最重要的部分是启用UIImageView 作为其用户交互。 isUserInteractionEnabled 默认设置为 false。上面的输出可以在下面看到,

    【讨论】:

    • @Visal Rajapakse - 非常感谢您提供如此准确的答案!请问您是否有可能在放置图钉上添加注释,是否有可能在我用来以 pdf 格式呈现图片的 WKWebView 上添加此功能?
    • 乐于助人:)。你所说的“drop pin”是什么意思,它是图像中的pin吗?除此之外,我对 WebViews 并不熟悉,但有一件事是,对于 pdf,您可以使用 Apples PDFKit,它为您提供了许多功能。
    • @Visal Rajapakse -谢谢 Visal!是的,我想知道是否可以为红色方块添加标题-副标题形式的文本,以便在我选择它时弹出。我正在阅读有关 PDFKit 的信息,但我找不到任何示例代码来进行实验......
    • 好吧,一切皆有可能:)。我还建议您查看UIPopover's,我相信他们可以使您将要做的事情变得非常容易。对于后者,我会推荐Raywenderlichs PDFKit 教程。但为了满足您的需求,您将不得不做一些额外的谷歌搜索
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多