当我将它添加到我的应用程序的 viewDidLoad 中时,我的注释不可见
My annonation isn't visible when I add it in the viewDidLoad of my app
我正在开发一个应用程序,用户必须在地图上标记一个位置,然后保存它。然后我的应用程序将其存档,当用户想要查看该位置时,该应用程序应该检索该位置的坐标,如果坐标存在,则在地图上添加一个注释。这是我创建的一段代码,用于显示用户选择的信息-
@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
部分中的信息时,图钉并没有像预期的那样放到地图上。
有人知道为什么我的代码没有按预期工作吗?谢谢!
如果您需要我的完整代码,请告诉我,我会把它放入我的问题中。
你应该把 mapViewFunc
放在 viewDidLoad
之外,但是如果你想把它放在 viewDidLoad
尝试
func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
// add pin
}
}
您需要使用
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
// configure the view
return annotationView
}
在您的 viewDidLoad()
函数之外
我正在开发一个应用程序,用户必须在地图上标记一个位置,然后保存它。然后我的应用程序将其存档,当用户想要查看该位置时,该应用程序应该检索该位置的坐标,如果坐标存在,则在地图上添加一个注释。这是我创建的一段代码,用于显示用户选择的信息-
@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
部分中的信息时,图钉并没有像预期的那样放到地图上。
有人知道为什么我的代码没有按预期工作吗?谢谢!
如果您需要我的完整代码,请告诉我,我会把它放入我的问题中。
你应该把 mapViewFunc
放在 viewDidLoad
之外,但是如果你想把它放在 viewDidLoad
尝试
func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
// add pin
}
}
您需要使用
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotationView")
// configure the view
return annotationView
}
在您的 viewDidLoad()
函数之外