Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过选择collectionView单元格在MapView上显示注释_Ios_Swift_Mapkit_Mapkitannotation - Fatal编程技术网

Ios 通过选择collectionView单元格在MapView上显示注释

Ios 通过选择collectionView单元格在MapView上显示注释,ios,swift,mapkit,mapkitannotation,Ios,Swift,Mapkit,Mapkitannotation,我有一个程序,可以根据所选的collectionView单元格在地图上显示注释。但当前,当尝试选择collectionView单元格时,地图上没有显示任何内容 这是代码 import UIKit import MapKit struct PlacesOnMap { var name: String var latitude: Double var longitude: Double init(name: String, latitude: Double, longitude: Double)

我有一个程序,可以根据所选的collectionView单元格在地图上显示注释。但当前,当尝试选择collectionView单元格时,地图上没有显示任何内容

这是代码

import UIKit
import MapKit

struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double

init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView1: MKMapView!
var placesVal = [PlacesOnMap(name: "place 1", latitude: 12.9716, longitude: 77.5946),
                 PlacesOnMap(name: "place 2", latitude: 12.2958, longitude: 76.6394),
PlacesOnMap(name: "place 3", latitude: 11.4102, longitude: 76.6950)
]
var buildings = [PlacesOnMap(name: "buildings 1", latitude: 12.9716, longitude: 77.5946),
PlacesOnMap(name: "buildings 2",  latitude: 12.2958, longitude: 76.6394)
]
var recreation = [PlacesOnMap(name: "recreation 1", latitude: 28.54693, longitude: -81.393071),
PlacesOnMap(name: "recreation 2", latitude: 28.538523, longitude: -81.385399),
PlacesOnMap(name: "recreation 3", latitude: 28.542817, longitude: -81.378117),
PlacesOnMap(name: "recreation 4", latitude: 28.538985, longitude: -81.404694)
]

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["Places", "Buildings", "Recreations"]
var displayCategoryText: String = ""


override func viewDidLoad() {
    super.viewDidLoad()

     mapView1?.delegate = self
}

// MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

// make a cell for each cell index path
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

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
    return cell
}

// MARK: - UICollectionViewDelegate protocol
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    displayCategoryText = String(indexPath.item)
    checkDisplayCategoryText()
}

func checkDisplayCategoryText() {
    if displayCategoryText == "0" {
        setPlacesAnnotations()
    }
    if displayCategoryText == "1" {
        setBuildingsAnnotations()
    }
    if displayCategoryText == "2" {
        setRecreationAnnotations()
    }
}

func setPlacesAnnotations() {
       let places = placesVal.map { placeOnMap -> MKPointAnnotation in
       let place = MKPointAnnotation()
       place.coordinate =  CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
       place.title = placeOnMap.name
       return place
       }
       mapView1?.removeAnnotations(mapView1.annotations)
       mapView1?.addAnnotations(places)
   }

   func setBuildingsAnnotations() {
       let places = buildings.map { placeOnMap -> MKPointAnnotation in
       let place = MKPointAnnotation()
       place.coordinate =  CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
       place.title = placeOnMap.name
       return place
       }
       mapView1?.removeAnnotations(mapView1.annotations)
       mapView1?.addAnnotations(places)
   }

   func setRecreationAnnotations() {
       let places = recreation.map { placeOnMap -> MKPointAnnotation in
       let place = MKPointAnnotation()
       place.coordinate =  CLLocationCoordinate2D(latitude: placeOnMap.latitude,  longitude: placeOnMap.longitude)
       place.title = placeOnMap.name
       return place
       }
       mapView1?.removeAnnotations(mapView1.annotations)
       mapView1?.addAnnotations(places)
   }


   func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let annotationTitle = view.annotation?.title else
              {
                  print("Unable to retrieve details")
               return
              }
     print("User tapped on annotation with title: \(annotationTitle!)")
   }

}
extension CollectionViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellsAcross: CGFloat = 3
    var widthRemainingForCellContent = collectionView.bounds.width
    if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout {
        let borderSize: CGFloat = flowLayout.sectionInset.left + flowLayout.sectionInset.right
        widthRemainingForCellContent -= borderSize + ((cellsAcross - 1) * flowLayout.minimumInteritemSpacing)
    }
    let cellWidth = widthRemainingForCellContent / cellsAcross
    return CGSize(width: cellWidth, height: cellWidth)
}

}

class MyCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var myLabel: UILabel!

}
这是故事板的照片


如何解决此问题,以便根据选择的集合视图单元格,在地图视图上显示正确的注释

我假设您已在情节提要中正确设置了
UICollectionView
委托和数据源

选择要设置的单元格时:

displayCategoryText = String(indexPath.item)

但是你不需要对这个变量做任何事情。您需要调用一个方法,该方法可以使用该变量的值从数据模型中提取正确的信息,然后将注释添加到地图中。

我假设您已在情节提要中正确设置了
UICollectionView
委托和数据源

选择要设置的单元格时:

displayCategoryText = String(indexPath.item)

但是你不需要对这个变量做任何事情。您需要调用一个方法,该方法可以使用该变量的值从数据模型中提取正确的信息,然后将注释添加到地图中。

我为此苦苦挣扎了整整3天,几乎阅读了谷歌的每一篇文章,直到找到了一篇有用的文章

首先在mapview中获取注释索引(didSelect:)

让index=(self.mapView.annotations作为NSArray)。index(of:anno)*anno是自定义注释视图的my view.annotation常量

然后,如果您希望collectionview通过点击注释滚动到正确的单元格,请在didSelect函数中添加以下内容:

让path=IndexPath(行:索引,节:0) self.collectionView.scrollToItem(在:路径,在:。中心水平,动画:true)

然后,在collectionView cellForItemAt中,我添加了以下内容:

self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row],动画:false)

这将选择与mapView注释的indexPath.row对应的注释


希望有帮助

我为此苦苦挣扎了整整3天,几乎阅读了谷歌的每一篇文章,直到找到了一篇有用的文章

首先在mapview中获取注释索引(didSelect:)

让index=(self.mapView.annotations作为NSArray)。index(of:anno)*anno是自定义注释视图的my view.annotation常量

然后,如果您希望collectionview通过点击注释滚动到正确的单元格,请在didSelect函数中添加以下内容:

让path=IndexPath(行:索引,节:0) self.collectionView.scrollToItem(在:路径,在:。中心水平,动画:true)

然后,在collectionView cellForItemAt中,我添加了以下内容:

self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row],动画:false)

这将选择与mapView注释的indexPath.row对应的注释


希望有帮助

viewDidLoad函数中是否没有通过“if displayCategoryText==”0“{setPlacesAnnotations()}”行调用变量?在viewDidLoad中,检查变量的值并调用setPlacesAnnotations,如果它等于“0”,则应用程序流将离开viewDidLoad而不再返回。在
didSelectItemAt
中所做的只是更改变量的值。这与ViewDidLoad中发生的检查没有关系,因为应用程序流不会返回到那里。这就是为什么您需要在更改后启动检查/方法。在更改后如何启动检查/方法?您需要编写一个函数,并在将新值设置为displayCatehoryText后调用它。您需要了解此变量的所有可能值,并在函数中检查实际值并执行相应操作。我已更新代码,以包含一个函数,该函数可检查displayCategoryText的可能情况,但是仍然没有在映射上获取任何批注。viewDidLoad函数中是否没有通过“if displayCategoryText==”0“{setPlacesAnnotations()}”行调用变量?在viewDidLoad中,检查变量的值并调用setPlacesAnnotations,它是否等于“0”但是,应用程序流离开ViewDidLoad,再也不会返回。在
didSelectItemAt
中所做的只是更改变量的值。这与ViewDidLoad中发生的检查没有关系,因为应用程序流不会返回到那里。这就是为什么您需要在更改后启动检查/方法。在更改后如何启动检查/方法?您需要编写一个函数,并在将新值设置为displayCatehoryText后调用它。您需要了解此变量的所有可能值,并在函数中检查实际值并执行适当的操作。我已更新了代码,以包含一个函数,该函数检查displayCategoryText的可能情况,但仍无法在映射上获得注释。