繁体   English   中英

调用时未在 map 视图上显示注释

[英]Annotations not being displayed on map view when called

这是与我的 storyboard 匹配的更新代码。 有两个不同的子视图控制器,map 视图和集合视图。 当用户单击允许卡片折叠和展开的按钮时,ContentViewController 还具有 animation。 mapView 是一个单独的视图。 问题是当我运行它时,当显示 animation 而不是只替换 MapViewController 时,会显示一个新视图 controller。 理想情况下,运行此程序时,用户将展开卡片视图,然后单击集合视图单元格,然后新的注释将填充屏幕顶部(因为这是 map 视图所在的位置)。 使用我当前的代码,注释不会显示在屏幕上。 我将如何解决这个问题,以便在单击单元格时可以看到受尊重的注释?

这是代码。

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 ContentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, MKMapViewDelegate {

@IBOutlet weak var mapView: 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)
      ]

//helps create an intial view for the mapView; zooms in on Orlando
   let startingLocation = CLLocation(latitude: 28.5383, longitude: -81.3792)
   let distanceSpan: CLLocationDistance = 4000

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

override func viewDidLoad() {
    super.viewDidLoad()

    zoomLevel(location: startingLocation)
}

//controls how much the map is zoomed in based off of intial inputs from the startingLocation variable
func zoomLevel(location: CLLocation) {
    let mapCoordinate = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: distanceSpan, longitudinalMeters: distanceSpan)
    mapView?.setRegion(mapCoordinate, animated: true)
}


    // 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! Cell

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

    // MARK: - UICollectionViewDelegate protocol
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
        if(indexPath.item == 0){
                  setPlacesAnnotations()
              }else if(indexPath.item == 1){
                  setBuildingsAnnotations()
        }else if(indexPath.item == 2){
                  setRecreationAnnotations()
              }
              mapView?.delegate = self
    }

    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
           }
           mapView?.removeAnnotations(mapView.annotations)
           mapView?.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
           }
           mapView?.removeAnnotations(mapView.annotations)
           mapView?.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
           }
           mapView?.removeAnnotations(mapView.annotations)
           mapView?.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!)")
       }

}

class Cell: UICollectionViewCell {
    @IBOutlet weak var textLabel: UILabel!
}

Storyboard。 在此处输入图像描述

您需要在一个ViewController中创建一个UICollectionView ,然后将索引值传递给另一个ViewController以将其显示在MKMapView上。

在这里创建了一个示例项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM