如何启用MapKit楼层级别?

时间:2019-02-12 11:00:44

标签: swift mapkit

我正在尝试将楼层显示在我的Apple应用程序中。我知道在苹果地图中,可以看到一些选定的地方,例如飞机场或购物中心。我需要做到这一点。只需显示可用的地板高度即可。正如您在图片中看到的那样,在图像的右侧有5F,4F,3F,2F等。我已经搜索了网,但还没有发现任何线索。

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要使用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")!)
  }
}