swift - UIImage 上的 MapKit 功能,放置图钉/注释
问题描述
我正在尝试在图像上使用现有的 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)")
}
}
任何帮助将不胜感激。谢谢乔治
解决方案
同样可以通过遵循相同的程序并在此处和那里进行一些调整来实现。我制作了一个示例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
。上面的输出可以在下面看到,
推荐阅读
- javascript - 可拖动的 div。如何在更改页面和重新加载后保持位置
- r - 闪亮的错误,没有输出
- php - 将 Laravel 请求错误拆分为多个部分
- excel - Sharepoint 上的 Excel 文件
- javascript - 如何使用 three.js 中的回调异步加载多个纹理?
- arrays - R - 如何在时间序列图像中找到掩码内的图像元素?
- javascript - 如果 json 有斜杠,JSON.parse 会抛出错误
- asp.net-mvc - 在 Asp.Net MVC 中的图像中绘制多行文本
- java - 使用 Checkstyle/PMD/Findbugs 检查变量值
- java - Wildfly10 EAR 部署在 Eclipse 中失败,但在standalone.bat 中工作