I'm trying to add a Pin at my map when the user does a longPress (with UILongPressGestureRecognizer), but this doesn't work. However, the bigger problem is: The map isn't displayed anymore. Why the map has disappeared?
You find the project-file 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)
}
}}
Because your map size it's {0,0} and you are not using any constrain.
Check here the project with a minor update https://dl.dropboxusercontent.com/u/19438780/test1-v2.zip