我想在用戶做一個longPress(使用UILongPressGestureRecognizer)的時候在我的地圖上添加一個Pin,但這不起作用。但是,更大的問題是:地圖不再顯示。爲什麼地圖消失了?如果我添加一個GestureRecognizer,MapView不起作用
你找到項目文件here
--------------------------- ViewController.swift ----- ---------------------------
{
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var theMapView: MKMapView!
@IBOutlet var theTextfield: UITextField!
@IBOutlet var theLabel: UILabel!
@IBAction func theButton(sender: UIButton) {
theLabel.text = "Swift-App schreibt \(theTextfield.text)"
theTextfield.resignFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Position
var latitude:CLLocationDegrees = 48.399193
var longitude:CLLocationDegrees = 9.993341
// Zoomfaktor
var latDelta:CLLocationDegrees = 0.01
var longDelta:CLLocationDegrees = 0.01
//
var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta)
// Koordinaten der Kirche
var churchLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
// Zentrum und Kartenausschnitt
var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(churchLocation, theSpan)
// LongTap definieren
let longpress = UILongPressGestureRecognizer(target: theMapView, action: "actionPin:")
longpress.minimumPressDuration = 1.0
longpress.numberOfTouchesRequired = 1
longpress.allowableMovement = 100
theMapView.addGestureRecognizer(longpress)
// Karte anzeigen
theMapView.setRegion(theRegion,animated:false)
// Pin setzen
var theUlmMinsterAnnotation = MKPointAnnotation()
theUlmMinsterAnnotation.coordinate = churchLocation
theUlmMinsterAnnotation.title = "Ulmer Münster"
theUlmMinsterAnnotation.subtitle = " Untertitel"
theMapView.addAnnotation(theUlmMinsterAnnotation)
}
func actionPin(gestureRecognizer:UIGestureRecognizer) {
var touchpoint = gestureRecognizer.locationInView(self.theMapView)
var newCoord:CLLocationCoordinate2D=theMapView.convertPoint(touchpoint, toCoordinateFromView: self.theMapView)
var newAnnotation = MKPointAnnotation()
newAnnotation.coordinate = newCoord
newAnnotation.title = "Fingertipp"
newAnnotation.subtitle = "Untertitel"
theMapView.addAnnotation(newAnnotation)
}
}}
項目如果您可以發佈相關的代碼,你可能會得到更好的反應。你所做的事聽起來不那麼複雜,以至於無法解決問題。 – Acey
@Acey完成了。謝謝! – Rico30
您是否正在使用storyboard/xib將'theMapView'添加到您的視圖控制器? – thelaws