我无法显示变量中的所有地址
var allAddress: [Address] = []
在变量中,我从 firebase 获取少量地址,我想在 annotationView 中显示所有地址,但是当我尝试显示所有地址我只看到所有 annotationView 中的一个地址,但如果我打印我的使用for..in .. 索引我看到 index0 , index1 , index2 , index3 等......如果我打印出来的话:
print("address - \(allAddress[index].address)")
我获得 firebase 中的所有地址,其总数 6 。
打印我的索引和 allAddress [index] .address
index 1
address - Москва, ул. Правды д.24, строение 3
index 2
address - Москва, ул.Электрозаводская д.21
index 3
address - Москва, ул.Бутырская д.8
index 4
address - Москва, 2-Я Звенигородская улица 12 строение 21
index 5
address - Москва, Николоямская 52, стр. 1
这是我的代码:
let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
for index in 0..<allAddress.count {
print("index \(index)")
centerInfo.text = allAddress[index].address
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping
print("address - \(allAddress[index].address)")
}
annotationView?.detailCalloutAccessoryView = centerInfo
我如何在centerInfo.text
显示所有地址?
请检查.gif,它在所有annotationView上显示相同的地址
P.S。在annotation.title中不需要使用,我不适合
已更新即可。所有代码:
class AllAddressMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var allAddress: [Address] = []
var studioRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
studioRef = Database.database().reference(withPath: "Photo1")
studioRef.observe(.value, with: { (snapshot) in
for imageSnap in snapshot.children {
let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)
self.allAddress.append(studioObj)
for index in 0..<self.allAddress.count {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(self.allAddress[index].address, completionHandler: { (placemarks, error) in
guard error == nil else { return }
guard let placemarks = placemarks else { return }
if let placemark = placemarks.first {
let annotation = MKPointAnnotation()
guard let address = placemark.location else { return }
annotation.coordinate = address.coordinate
self.mapView.addAnnotation(annotation)
}
geocoder.cancelGeocode()
})
}
}
})
mapView.delegate = self
mapView.mapType = .standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
let annotationID = "PinMap"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
let leftImageNavigationButton = UIImage(named: "auto")
let tintedColorleftImageNavigationButton = leftImageNavigationButton?.withRenderingMode(.alwaysTemplate)
let leftNavigationButton = UIButton(type: .custom)
leftNavigationButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftNavigationButton.setImage(tintedColorleftImageNavigationButton, for: UIControlState())
leftNavigationButton.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
annotationView?.leftCalloutAccessoryView = leftNavigationButton
let rightButtonInfo = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = rightButtonInfo
let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
for index in 0..<allAddress.count {
print("index \(index)")
centerInfo.text = allAddress[index].address
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping
print("address - \(allAddress[index].address)")
}
annotationView?.detailCalloutAccessoryView = centerInfo
annotationView?.calloutOffset = CGPoint(x: -8, y: 0)
return annotationView
}
}
答案 0 :(得分:0)
从您的代码看,您对所有注释使用相同的UILabel
,并且在循环中每次都设置相同text
的{{1}}属性。这就是为什么它显示所有注释的最后UILabel
。
尝试为每个注释分配一个单独的address
,并将UILabel
属性设置为指定的text
答案 1 :(得分:0)
好吧,我的想法是在你的地图上获得所有注释,并尝试找到正确的注释索引,将其重用到你的allAddress数组中:
var indexValue = 0
for annotationInMap in mapView.annotations {
if annotation == annotationInMap {
let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
centerInfo.text = allAddress[indexValue].address
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping
annotationView?.detailCalloutAccessoryView = centerInfo
}
indexValue = indexValue + 1
}
答案 2 :(得分:0)
如何在viewDidLoad中将地址设置为annotation.title
,并在viewFor方法中获取annotation.title
。
class AllAddressMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var allAddress: [Address] = []
var studioRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
studioRef = Database.database().reference(withPath: "Photo1")
studioRef.observe(.value, with: { (snapshot) in
for imageSnap in snapshot.children {
let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)
self.allAddress.append(studioObj)
for index in 0..<self.allAddress.count {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(self.allAddress[index].address, completionHandler: { (placemarks, error) in
guard error == nil else { return }
guard let placemarks = placemarks else { return }
if let placemark = placemarks.first {
let annotation = MKPointAnnotation()
guard let address = placemark.location else { return }
annotation.coordinate = address.coordinate
annotation.title = self.allAddress[index].address
self.mapView.addAnnotation(annotation)
}
geocoder.cancelGeocode()
})
}
}
})
mapView.delegate = self
mapView.mapType = .standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
let annotationID = "PinMap"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
let leftImageNavigationButton = UIImage(named: "auto")
let tintedColorleftImageNavigationButton = leftImageNavigationButton?.withRenderingMode(.alwaysTemplate)
let leftNavigationButton = UIButton(type: .custom)
leftNavigationButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftNavigationButton.setImage(tintedColorleftImageNavigationButton, for: UIControlState())
leftNavigationButton.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
annotationView?.leftCalloutAccessoryView = leftNavigationButton
let rightButtonInfo = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = rightButtonInfo
let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
for index in 0..<allAddress.count {
print("index \(index)")
centerInfo.text = annotation.title
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping
print("address - \(allAddress[index].address)")
}
annotationView?.detailCalloutAccessoryView = centerInfo
annotationView?.calloutOffset = CGPoint(x: -8, y: 0)
return annotationView
}