[英]MKMarkerAnnotationView don't show on iPhone X
在舊手機(iPhone 7、8)上,為MKMarkerAnnotationView
創建的MKMapSnapshotter
在 map 上正確顯示。 僅在 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!)
}
}
}
}
任何人都知道是什么原因導致它們不出現在較新的手機上?
謝謝^.^
因此,解決方案是在生成 map 和視圖時,它需要在應用程序處於活動狀態時發生。 對於新手機,嘗試在后台生成視圖是行不通的。 在舊手機上會。 為什么? 我不知道。 因此,需要發生的是 map 生成只需要在應用程序處於活動狀態時發生。 這樣做會在所有設備上產生一致的生成。
我一直未能再次找到 SDK 文檔中發布的位置,但這就是我找到解決方案的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.