Swift/iOS 项目地图无法正确显示位置

问题描述

大家好,我在测试 Swift 应用程序中遇到地图实现问题。所以我像下面的代码一样添加了地图视图,然后作为预览数据我传递了一个随机城市的坐标。然后我运行了实时预览,并且(非常失望)看到了蓝色矩形而不是地图。我搞砸了什么?

enter image description here

import MapKit
    struct MapSection: View {
        private var log: Log?
        
        @State var region: MKCoordinateRegion = MKCoordinateRegion()
        private var locationCoordinate: CLLocationCoordinate2D?
        
        init(_ log: Log) {
            self.log = log
            if let coordinate = log.location?.coordinate {
                let locationCoordinate = CLLocationCoordinate2D(latitude: coordinate.latitude,longitude: coordinate.longitude)
                self.locationCoordinate = locationCoordinate
                self.region = MKCoordinateRegion(center: locationCoordinate,span: MKCoordinateSpan(latitudeDelta: 0.075,longitudeDelta: 0.075))
            } else {
                self.log = nil
                self.locationCoordinate = nil
            }
        }
        
        var body: some View {
            Section(header: Text("Position on map")) {
                if log != nil && locationCoordinate != nil {
                    Map(coordinateRegion: $region,annotationItems: [self.log!]) {
                        _ in MapPin(coordinate: locationCoordinate!)
                    }
                        .frame(minHeight: 500)
                }
                else {
                    HStack {
                        Spacer()
                        Text("Map Error: Position UnkNown")
                        Spacer()
                    }
                }
            }
        }
    }

struct LogDetail_Previews: PreviewProvider {
    static var previews: some View {
        let context = PersistenceController.preview.container.viewContext
        let log = Log(context: context)
        log.country = Locale.current.regionCode
        log.date = Date(timeIntervalSince1970: TimeInterval(100000))
        log.location = CLLocation(latitude: CLLocationdegrees(52.229676),longitude: CLLocationdegrees(21.012229))
        return LogDetail(log)
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...