2014-11-13 77 views
2

我想在用戶做一個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) 
} 

}} 
+0

項目如果您可以發佈相關的代碼,你可能會得到更好的反應。你所做的事聽起來不那麼複雜,以至於無法解決問題。 – Acey

+0

@Acey完成了。謝謝! – Rico30

+0

您是否正在使用storyboard/xib將'theMapView'添加到您的視圖控制器? – thelaws

回答

0

由於您的地圖大小爲{0,0},因此您沒有使用任何限制。

入住這裏有一個小更新https://dl.dropboxusercontent.com/u/19438780/test1-v2.zip

+0

謝謝,現在我看到了地圖。約束似乎是學習一些有關的重要話題。 :)但是你有什麼想法爲什麼長期不被認可? – Rico30

+0

在MKMapView上添加長按並不是那麼簡單,因爲已經有其他UIGestureRecognizer了......一種解決方案可以是...繼承MKMapView http://stackoverflow.com/a/21124351/1702413 ...或試圖「檢測」這些現有的手勢,並要求所有的KeepestRecognizerToFail – TonyMkenu

相關問題