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