2016-11-21 15 views
0

我正在開發一個應用程序,用戶必須在地圖上標記位置,然後保存它。然後,我的應用程序將其歸檔,當用戶想要查看該位置時,該應用程序應該檢索該位置的座標,並在存在座標時在地圖上添加一個annonation。下面是我創建的顯示信息的用戶selected-當我將其添加到我的應用程序的viewDidLoad中時,我的annonation不可見

@IBOutlet var mapGestureRecognizer: UIGestureRecognizer! 
var item: itemData? 

// Item Outlets 
@IBOutlet weak var itemInfoView: UIView! 
@IBOutlet weak var itemNameTextField: UITextField! 
@IBOutlet weak var itemDescriptionLabel: UITextView! 
@IBOutlet weak var mapView: MKMapView! 

// Item Location Outlets 
@IBOutlet weak var itemLocationView: UIView! 
@IBOutlet weak var itemLocationTextView: UITextView! 

let dropPin = MKPointAnnotation() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.mapView.delegate = self 

    // Data loading 
    itemNameTextField.delegate = self 
    itemDescriptionLabel.delegate = self 
    itemLocationTextView.delegate = self 

    // Press recognizer 

    func mapViewFunc(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView") 
     // configure the view 

     return annotationView 
    } 

    if let item = item { 

     itemNameTextField.text = item.itemName 
     itemDescriptionLabel.text = item.itemDescription 
     itemLocationTextView.text = item.itemPlace 

     dropPin.coordinate = mapView.convert(item.mapPoint, toCoordinateFrom: mapView) 
     dropPin.title = "Location of \(item.itemName)" 
     mapView.addAnnotation(dropPin) 
     print("Set the location of item pin to \(String(describing: dropPin.coordinate))") 


    } 

    // Styles 
    itemInfoView.layer.cornerRadius = 3 
    itemInfoView.layer.shadowColor = UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor 
    itemInfoView.layer.shadowOffset = CGSize(width: 0, height: 1.75) 
    itemInfoView.layer.shadowRadius = 1.7 
    itemInfoView.layer.shadowOpacity = 0.45 

    itemLocationView.layer.cornerRadius = 3 
    itemLocationView.layer.shadowColor = UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor 
    itemLocationView.layer.shadowOffset = CGSize(width: 0, height: 1.75) 
    itemLocationView.layer.shadowRadius = 1.7 
    itemLocationView.layer.shadowOpacity = 0.45 

    } 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 
let annotation = MKPointAnnotation() 
//let point = mapView.convert(coordinate, toPointTo: overlayView) 
func action(gestureRecognizer:UIGestureRecognizer){ 

    if let item = item { 
     dropPin.coordinate = mapView.convert((item.mapPoint), toCoordinateFrom: mapView) 
     dropPin.title = "Location of \(item.itemName)" 
     mapView.addAnnotation(dropPin) 
     print("Set the location of item pin to \(String(describing: dropPin.coordinate))") 
    } 

    else { 
     let itemName = itemNameTextField.text 
     let touchPoint = gestureRecognizer.location(in: mapView) 
     let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView) 
     annotation.coordinate = newCoordinates 
     annotation.title = "Location of \(itemName!)" 
     mapView.addAnnotation(annotation) 
    } 

} 

@IBAction func mapLocationPress(_ sender: UILongPressGestureRecognizer) { 
     action(gestureRecognizer: mapGestureRecognizer) 

} 

所有打印我創建工作報表一段代碼,讓我知道,應用程序確實保存座標,它是否正確轉換它們。但是,當應用程序加載viewDidLoadif let item = item部分中的信息時,該引腳不會像它應該放在地圖上一樣。

有沒有人看到我的代碼爲什麼沒有按照它應該的方式工作?謝謝!

如果你需要我的完整代碼,告訴我,我會把它放到我的問題。

+0

五月是地圖沒有被加載,直到viewDidLoad之後,你有沒有嘗試在viewDidAppear或viewWillAppear中添加註釋? –

+0

也把'func mapViewFunc'從viewDidLoad – Rajat

回答

1

您需要使用

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView") 
     // configure the view 

     return annotationView 
} 

viewDidLoad()功能

+0

我把這個函數移到了我的'viewDidLoad()'之外,但是當屏幕加載時它仍然不出現,儘管它_does_保存座標,並且它_does_分配座標到銷。它可能是別的嗎? –

+0

你在這裏得到了什麼 - 'print(「將項目引腳的位置設置爲\(字符串(描述:dropPin.coordinate))))' – Rajat

+0

當這個語句運行時,xCode打印'設置項目引腳的位置爲CLLocationCoordinate2D(緯度:73.632655333854188,經度:-104.16040759876424)'(這些是我掉落的針的座標) –

0

你應該把mapViewFuncviewDidLoad,但如果你想將它留在viewDidLoad

嘗試

func viewDidLoad() { 
    super.viewDidLoad() 
    DispatchQueue.main.async { 
     // add pin 
    } 
} 
相關問題