2016-11-11 68 views
0

在我的應用程序中,用戶保存一些數據,包括地圖座標。在我的代碼中,一個別針被放在保存的地圖座標上。這裏是我的代碼 -爲什麼我的MKPointAnnotation在視圖加載時不顯示?

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.mapView.delegate = self 

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

    // Press recognizer 

    if let item = item { 

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

     let dropPin = MKPointAnnotation() 
     dropPin.coordinate = mapView.convert(item.mapPoint, toCoordinateFrom: mapView) 
     dropPin.title = "Location of \(item.itemName)" 
     self.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 

    locationAdressTextview.layer.cornerRadius = 2 
    locationAdressTextview.layer.shadowColor = UIColor(red:0/255.0, green:0/255.0, blue:0/255.0, alpha: 1.0).cgColor 
    locationAdressTextview.layer.shadowOffset = CGSize(width: 0, height: 1.50) 
    locationAdressTextview.layer.shadowRadius = 1.6 
    locationAdressTextview.layer.shadowOpacity = 0.3 
} 

我知道,應用程序確實保存銷座標作爲CGPoint,我知道它從一個CGPoint將其轉化爲CLLocationCoordinate2D,因爲print報表一放置。但是,當屏幕加載時,print語句會顯示一個有效的座標,但地圖上沒有pin,並且不會出現錯誤。有人能幫幫我嗎?謝謝!

+0

如果您需要澄清,或者您需要我添加更多的代碼,只需發表評論。 –

回答

1

每當地圖應顯示的註解MKMapViewDelegate方法viewForAnnotation被調用,所以你必須實現該方法,並根據您的需要

示例代碼返回一個觀點:

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

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

    return annotationView 
} 
+0

對不起,但我對swift比較陌生。 'PinAnnonationView'指的是什麼? –

+0

對不起,我的錯。我已經更新了我的答案。你可以繼承'MKAnnotationView'來添加你的需求。 – zisoft

+0

你會對代碼的其餘部分做出什麼改變? –

相關問題