微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 mapType 设置为 SatelliteFlyover 时调用 mapViewDidFailLoadingMap(_:withError)?

如何解决如何在 mapType 设置为 SatelliteFlyover 时调用 mapViewDidFailLoadingMap(_:withError)?

我正在使用 MapView 并希望在由于设备离线(或由于任何其他原因无法加载图块)而无法加载地图时显示静态图像。

实际上这在使用 MKMapViewDelegate 和实现 mapViewDidFailLoadingMap(_:withError) 时有效。

在我的例子中,MKMapType.satelliteFlyover,这以某种方式阻止了委托方法调用。例如,它适用于 .satellite

我已通过关闭任何互联网连接在模拟器和设备上对此进行了测试。控制台打印错误

[ResourceLoading] 无法加载密钥:34.21.6 t:4 kt:0 类型:4,-1009: NSURLErrorDomain 错误域=NSURLErrorDomain 代码=-1009 “ Internet 连接似乎处于脱机状态。” UserInfo={NSLocalizedDescription=Internet 连接似乎是 离线。但没有其他事情发生。

我做错了吗?如果不是:我怎样才能实现正确检测错误的目标? (我不想发出 URL 请求或使用连接检测)

这是我使用 SwiftUI 和包装为 MKMapViewUIViewRepresentable 的示例实现。只需在运行前关闭所有互联网连接即可。

import SwiftUI
import MapKit

struct ContentView: View {
    var body: some View {
        RepresentableMapView()
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct RepresentableMapView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        mapView.mapType = .satelliteFlyover
        return mapView
    }

    func updateUIView(_ view: MKMapView,context: Context) {

    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject,MKMapViewDelegate {
        var parent: RepresentableMapView

        init(_ parent: RepresentableMapView) {
            self.parent = parent
        }

        func mapViewDidFinishRenderingMap(_ mapView: MKMapView,fullyRendered: Bool) {
            print("mapViewDidFinishRenderingMap")
        }

        func mapViewDidFailLoadingMap(_ mapView: MKMapView,withError error: Error) {
            print(error)
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。