我正試圖在一張圖片上使用現有的MapKit功能。 我想實作的是在圖片上放置圖釘,并在這些圖釘上添加注釋。 我已經設法為用戶提供了通過長手勢動態添加圖釘的可能性,但我不知道如何在圖片上實作同樣的功能。
我的代碼如下:
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(目標。self, action: #selector(self.handleTap(_:))
longPressRecogniser.minimumPressDuration=0.5
mapView.addGestureRecognizer(longPressRecogniser)
mapView.mapType = MKMapType.standard
let location = CLLocationCoordinate2D( latitude: CLLocationDegrees(keyLat.toFloat()),經度。CLLocationDegrees(keyLon.toFloat()))
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(cent: location, span: span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
注解.坐標 = 位置
注釋.標題 = "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)
//>添加注釋:
let annotation = MKPointAnnotation()
注解.坐標 =坐標
annotation.title = "latitude:" String(格式。"%.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(格式。"%.02f",Float((view.annotation?.coordinate.longitude)!)
print("latitude: (latValStr) & longitude: (lonvalStr)")
}
}
非常感謝任何幫助。 謝謝 喬治
uj5u.com熱心網友回復:
按照同樣的程式,在這里和那里做一些調整,也可以達到同樣的效果。我做了一個示例ViewController,演示了如何在UIImageView中添加指標(UIViews)。
class ViewController。UIViewController {
@IBOutlet weak var imageView: UIImageView! //影像視圖!
lazy var longPress = UILongPressGestureRecognizer(目標。self, action: #selector(didLongPressScreen)) //長按的手勢。
//MARK: 生命周期[/span>
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated。Bool) {
super.viewDidAppear(animated)
設定()
}
//MARK: 函式[/span>
private func setup() {
imageView.image = UIImage(named: "photo"/span>)
imageView.addGestureRecognizer(longPress) //添加手勢識別器。
imageView.isUserInteractionEnabled = true //ImageViews默認不是用戶互動的。
}
//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 //將視圖的中心設定為長按的x,y坐標。
self.view.addSubview(pointer) //將UIView添加到視圖中。
}
}
}
其中最重要的部分是為UIImageView啟用用戶互動,因為其。isUserInteractionEnabled默認設定為false。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307297.html
標籤:

