【发布时间】: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