需要帮助将mapKit注释链接到Swift

时间:2017-04-05 21:09:48

标签: ios swift annotations uicollectionview mapkit

我正在swift中构建一个应用程序,它在同一页面上有一个集合视图和一个地图视图。用户可以滚动浏览集合视图以查看商品(这是应用程序的用途),地图视图中填充了注释引脚,用于在地图上显示商品的实际位置。

我的问题是我发现很难将集合视图单元格链接到正确的注释。

我将注释添加到地图中,如下所示:

for location in self.queryDataMapAnnotations {
            let annotation = MKPointAnnotation()
            annotation.title = location["title"] as? String
            annotation.subtitle = location["distance"] as? String
            annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
            self.mapView.addAnnotation(annotation)

        }

然后我可以滚动浏览集合视图单元格

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell


        // Connect cell to the linked annotation 

        // ** This links to a random annotation, but not the correct one - need help on this please ** 
        self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true)

    }

如果任何人可以帮助我如何将集合视图链接到正确的地图注释,我会非常有帮助。我只知道快速(一点点)所以会很感激那种语言的帮助。谢谢

2 个答案:

答案 0 :(得分:2)

如果您希望在滚动时更改活动地图注释,并且您希望将垂直居中的集合单元格视为当前显示的内容 - 这就是我想出的方式。

注意:我没有测试它,这只是基本的想法

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView is UICollectionView {
        let collectionViewCenter = CGPoint(x: collectionView.bounds.size.width / 2 + collectionView.contentOffset.x, y: collectionView.bounds.size.height / 2 + collectionView.contentOffset.y)
        let centredCellIndexPath = collectionView.indexPathForItem(at: collectionViewCenter)

        guard let path = centredCellIndexPath else {
            // There is no cell in the center of collection view (you might want to think what you want to do in this case)
            return
        }

        if let selectedAnnotation = mapView.selectedAnnotations.first {
            // Ignore if correspondent annotation is already selected
            if selectedAnnotation.isEqual(self.mapView.annotations[path.row]) {
                self.mapView.selectAnnotation(self.mapView.annotations[path.row], animated: true)
            }
        }
    }
}

答案 1 :(得分:0)

我有类似的功能,用户滚动收集视图,显示附近交易的列表,mapview的相机应该转移到集合视图中当前可见交易的位置,下面描述我的实现

    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){
        /* set pagewidth to collectionview cell width*/
        let pageWidth = collectionView.bounds.width 
        let currentOffset : Float = Float(scrollView.contentOffset.x)
        let targetOffset = Float(targetContentOffset.memory.x)
        var newTargetOffset : Float = 0.0

        if targetOffset > currentOffset {
            newTargetOffset = ceilf(currentOffset/(pageWidth)) * pageWidth
            /* if colelctionview swipped right increment the curentlyVisibleIndex*/
       //dealList is array of Model objects used to populate the 
              //CollectionView
            if currentlyVisibleIndex != (dealList.count - 1) {
                currentlyVisibleIndex += 1
            }
        }
        else{
            newTargetOffset = floorf(currentOffset/(pageWidth)) * pageWidth
            /*if collectionview swipped left decrement the currentlyVisibleCounter */
            if currentlyVisibleIndex !=  0 {
                currentlyVisibleIndex -= 1
            }
        }
        if newTargetOffset < 0{
            newTargetOffset = 0
        }else if  newTargetOffset > Float(scrollView.contentSize.width){
            newTargetOffset = Float(scrollView.contentSize.width)
        }
        targetContentOffset.memory.x = CGFloat(currentOffset)
        scrollView.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: 0), animated: true)

        moveCameraToDealLocation()
        // re initialise the annotations of mapView
        let annotationArray = mapBoxViewController.mapView.annotations
        mapBoxViewController.mapView.removeAnnotations(annotationArray)
        mapBoxViewController.mapView.addAnnotations(annotationArray)
    }

    func moveCameraToDealLocation(){
        mapBoxViewController.moveMapViewCameraToLocation(CLLocationCoordinate2D(latitude: dealList[currentlyVisibleIndex].eventLatitude, longitude: dealList[currentlyVisibleIndex].eventLongitude))
    }

currentVisibleIndex给出了当前可见交易(注释)的索引,函数moveCameraToDealLocation将MapView相机移动到该注释。 请告诉我这是否适合您:)