Swift 如何启用MapKit楼层级别?

Swift 如何启用MapKit楼层级别?,swift,mapkit,Swift,Mapkit,我正在尝试在我的苹果应用程序中显示楼层。我知道在苹果地图上有一些特定的地方,比如机场或购物中心,可以看到这层楼。我需要做到这一点。只需显示可用的楼层高度。正如你在图片中看到的,在图片的右侧有5F、4F、3F、2F等。我已经搜索了网络,但左边还没有任何线索 您需要使用MKOverlay。您可以将每个楼层作为覆盖添加到MKMapView中,并显示用户选择的任何楼层,隐藏其他楼层 下面是制作覆盖图的示例: import MapKit class MapOverlay: NSObject, MKOve

我正在尝试在我的苹果应用程序中显示楼层。我知道在苹果地图上有一些特定的地方,比如机场或购物中心,可以看到这层楼。我需要做到这一点。只需显示可用的楼层高度。正如你在图片中看到的,在图片的右侧有5F、4F、3F、2F等。我已经搜索了网络,但左边还没有任何线索


您需要使用MKOverlay。您可以将每个楼层作为覆盖添加到MKMapView中,并显示用户选择的任何楼层,隐藏其他楼层

下面是制作覆盖图的示例:

import MapKit

class MapOverlay: NSObject, MKOverlay {
  var coordinate: CLLocationCoordinate2D
  var boundingMapRect: MKMapRect

  override init() {
    let location = CLLocationCoordinate2D(latitude: 75.3307, longitude: -152.1929) // change these for the position of your overlay
    let mapSize = MKMapSize(width: 240000000, height: 200000000) // change these numbers for the width and height of your image
    boundingMapRect = MKMapRect(origin: MKMapPoint(location), size: mapSize)
    coordinate = location
    super.init()
  }
}

class MapOverlayRenderer: MKOverlayRenderer {
  let overlayImage: UIImage
  init(overlay: MKOverlay, image: UIImage) {
    self.overlayImage = image
    super.init(overlay: overlay)
  }

  override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
    guard let imageReference = overlayImage.cgImage else { return }

    let rect = self.rect(for: overlay.boundingMapRect)
    context.scaleBy(x: 1.0, y: -1.0)
    context.translateBy(x: 0.0, y: -rect.size.height)
    context.draw(imageReference, in: rect)
  }
}
然后将其添加到地图中:

let mapOverlay = MapOverlay()
mapView.addOverlay(mapOverlay)
不要忘记代表:

mapView.delegate = self

extension ViewController: MKMapViewDelegate {
  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    return MapOverlayRenderer(overlay: overlay, image: UIImage(named: "overlayImage")!)
  }
}