我尝试在用户执行longPress(使用UILongPressGestureRecognizer)时在地图上添加Pin,但这并不起作用。但是,更大的问题是:地图不再显示。为什么地图消失了?
您找到项目文件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)
}
}}
答案 0 :(得分:0)
因为您的地图尺寸为{0,0}并且您没有使用任何约束。
在此处检查项目,并进行较小的更新https://dl.dropboxusercontent.com/u/19438780/test1-v2.zip