我正在開發一個應用程序,用戶必須在地圖上標記位置,然後保存它。然後,我的應用程序將其歸檔,當用戶想要查看該位置時,該應用程序應該檢索該位置的座標,並在存在座標時在地圖上添加一個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)
}
所有打印我創建工作報表一段代碼,讓我知道,應用程序確實保存座標,它是否正確轉換它們。但是,當應用程序加載viewDidLoad
的if let item = item
部分中的信息時,該引腳不會像它應該放在地圖上一樣。
有沒有人看到我的代碼爲什麼沒有按照它應該的方式工作?謝謝!
如果你需要我的完整代碼,告訴我,我會把它放到我的問題。
五月是地圖沒有被加載,直到viewDidLoad之後,你有沒有嘗試在viewDidAppear或viewWillAppear中添加註釋? –
也把'func mapViewFunc'從viewDidLoad – Rajat