在iOS swift中,当集合视图滑动时如何调用地图注释?
在我的项目中,我使用MK map和uicollection视图。在地图视图中,显示基于用户当前位置的最近用户朋友位置地图注释。在集合视图中,与用户朋友相同的位置名称和距离显示在UICollection视图中。(注:对于收藏,我使用了UPCarouselFlowLayout cocopod图书馆) 在这里,我想突出显示距离用户当前位置最近的地图注释标记,并在地图中调用和选择wile SWING集合地图注释 下面是我为最近的用户朋友位置和集合视图尝试的代码:在iOS swift中,当集合视图滑动时如何调用地图注释?,ios,swift,uicollectionview,mkmapview,mkannotationview,Ios,Swift,Uicollectionview,Mkmapview,Mkannotationview,在我的项目中,我使用MK map和uicollection视图。在地图视图中,显示基于用户当前位置的最近用户朋友位置地图注释。在集合视图中,与用户朋友相同的位置名称和距离显示在UICollection视图中。(注:对于收藏,我使用了UPCarouselFlowLayout cocopod图书馆) 在这里,我想突出显示距离用户当前位置最近的地图注释标记,并在地图中调用和选择wile SWING集合地图注释 下面是我为最近的用户朋友位置和集合视图尝试的代码: func mapView(_ mapVi
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
let pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.image = UIImage(named: "carIcon")
userPinView = pin
return pin
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
}
else {
annotationView!.annotation = annotation
}
print("Get parking array count",parkingTypeArray.count)
if parkingTypeArray.count > 0 {
print("parking ty \(parkingTypeArray)")
//
for cameraa in parkingTypeArray.enumerated() {
if cameraa.element == "Free" {
let pinImage = UIImage(named: "free")
annotationView!.image = pinImage
}else if cameraa.element == "Paid" {
let pinImage = UIImage(named: "paid")
annotationView!.image = pinImage
}
}
}
return annotationView
}
下面是集合视图的代码:
//UICollection view - Location for parking car and street name
extension HomeViewController : UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cameraDetails.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = nearestCurrentLocationCollection.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! nearestLocationCollectionViewCell
if self.cameraImageArray.count > 0 {
cell.locationImage.sd_setImage(with: URL(string:
self.cameraImageArray[indexPath.row]))
print("get location image",URL(string:
self.cameraImageArray[indexPath.row]))
}
if self.cameraDetails.count > 0 {
cell.locationName.text = self.cameraDetails[indexPath.row]
}
// cell.locationDistance.text = locationDistance[indexPath.row]
if distanceArray.count > 0 {
cell.locationDistance.text = String(format: " Distance : %.2f ", distanceArray[indexPath.row]/1000)
}
// self.activityIndicator.stopAnimating()
// self.activityIndicator.isHidden = true
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
moveMapPage()
}
fileprivate var pageSize: CGSize {
let layout = self.nearestCurrentLocationCollection.collectionViewLayout as! UPCarouselFlowLayout
var pageSize = layout.itemSize
if layout.scrollDirection == .horizontal {
pageSize.width += layout.minimumLineSpacing
} else {
pageSize.height += layout.minimumLineSpacing
}
return pageSize
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let layout = nearestCurrentLocationCollection.collectionViewLayout as! UPCarouselFlowLayout
let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
print("currentpage::::\(self.currentPage)")
if let selectedAnnotation = mapView.selectedAnnotations.first {
// Ignore if correspondent annotation is already selected
if
selectedAnnotation.isEqual(self.mapView.annotations[currentPage]) {
self.mapView.selectAnnotation(self.mapView.annotations[currentPage], animated: true)
}
}
}
}
像这样试试它对我有用
UICollectionView
委托方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
for annotations_item in mapView.annotations{
if annotations_item.title == Arr_Map_Data[indexPath.row].Title{
self.mapView.selectAnnotation(annotations_item, animated: true)
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// first ensure that it really is an EventAnnotation:
// Note :- Here get your Annotation I used MyPointAnnotation custom class that's why use that
if let eventAnnotation = view.annotation as? MyPointAnnotation {
if let index = self.Arr_Map_Comparables.index(of: eventAnnotation.obj!){
let indexPath = IndexPath(row: index, section: 0)
collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
}
}
}
地图视图委托方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
for annotations_item in mapView.annotations{
if annotations_item.title == Arr_Map_Data[indexPath.row].Title{
self.mapView.selectAnnotation(annotations_item, animated: true)
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// first ensure that it really is an EventAnnotation:
// Note :- Here get your Annotation I used MyPointAnnotation custom class that's why use that
if let eventAnnotation = view.annotation as? MyPointAnnotation {
if let index = self.Arr_Map_Comparables.index(of: eventAnnotation.obj!){
let indexPath = IndexPath(row: index, section: 0)
collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
}
}
}
除了当前页面之外,您是否还获得了任何其他注释信息,如标题或其他信息?没有。。我没有得到任何错误。以上是否正确?@PvDev从您要选择批注动画的位置,我必须使用did selected方法执行此操作。在滑动单元格时,此操作是否适用于集合视图。对于集合视图滑动手势,请尝试此“抱歉,不要集合滑动”。滚动集合视图时,所选索引应选择地图annotation@PvDev您想在选定的集合视图单元格上显示选定的地图批注,并滚动到选定地图批注的集合视图单元格,对吗?