MKMarkerAnnotationView在iPhone X上不显示

时间:2019-10-29 18:42:26

标签: ios swift iphone mapkit

在较旧的电话(iPhone 7、8)上,为MKMarkerAnnotationView创建的MKMapSnapshotter在地图上正确显示。仅在iPhone X及更高版本上它们不显示。我不知道可能是什么问题。这是创建快照的代码

static func generateMap(coordinates: [CLLocationCoordinate2D], id: String){

  var annotationViews = [MKMarkerAnnotationView]()
  let startCoord = coordinates.first!
  let endCoord = coordinates.last!

  ///create mapmarker
  func createMapMarkerAnnotation(text: String, coordinates: CLLocationCoordinate2D, color: UIColor) -> MKMarkerAnnotationView{
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinates

        let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "marker")
        annotationView.glyphText = text
        annotationView.glyphTintColor = .white
        annotationView.markerTintColor = color
        annotationView.contentMode = .scaleAspectFit
        annotationView.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
        return annotationView
    }

   //CREATE MARKERS
   annotationViews.append(createMapMarkerAnnotation(text: "A", coordinates: startCoord, color: Constants.primaryGreen))
   annotationViews.append(createMapMarkerAnnotation(text: "B", coordinates: endCoord, color: Constants.primaryGreen))

  let snapshotter = MKMapSnapshotter(options: options)
    snapshotter.start { (snapshot: MKMapSnapshotter.Snapshot?, error: Error?) -> Void in
        guard error == nil, let snapshot = snapshot else { return }

        let image = snapshot.image
        UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);
        image.draw(at: CGPoint.zero) //image into context


        /// get the context for CoreGraphics
        let context = UIGraphicsGetCurrentContext()

        ///draw markers
        for view in annotationViews {
            let point: CGPoint = snapshot.point(for: view.annotation!.coordinate)
            view.drawHierarchy(in: CGRect(x: point.x - view.bounds.size.width / 2, y: point.y - view.bounds.size.height, width: view.bounds.width, height: view.bounds.height), afterScreenUpdates: true)
        }

        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let img = finalImage {
            let data: Data? = img.pngData()
            let content = data?.base64EncodedString()

            //write to db
            _ = DbTrips.shareInstance.updateTripMap(id: id, map: content!)
        }
    }
  }
}

任何人都不知道是什么原因导致它们无法出现在较新的手机上?

谢谢^。^

1 个答案:

答案 0 :(得分:0)

因此,解决方案是在生成地图和视图时,需要在应用程序处于活动状态时发生。对于新手机,尝试在后台生成视图将不起作用。在较旧的手机上会。为什么?我不知道。因此,需要发生的是仅在应用程序处于活动状态时才需要生成地图。这样做可以在所有设备上产生一致的生成。

我一直未能成功找到SDK文档中发布的内容,但这是我找到解决方案的地方。