mapkit - 在地图中包裹纬度和经度时崩溃
问题描述
我获取当前用户地图位置并将其显示在以他的位置为地图中心的地图上。上线就崩溃了
let currentLatitude = (locationManager.location?.coordinate.latitude)!
let currentLongitude = (locationManager.location?.coordinate.longitude)!
出现错误“无法从角落 4 插入法律归属”
我认为它与纬度和经度的强制换行有关。我该怎么做才能解决这个错误??
这是我的代码:
// Location Manager settings
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let currentLatitude = (locationManager.location?.coordinate.latitude)!
let currentLongitude = (locationManager.location?.coordinate.longitude)!
//Map settings
mapMyLocation.showsUserLocation = true
mapMyLocation.delegate = self
let locationcoordinates = CLLocationCoordinate2D(latitude: currentLatitude, longitude: currentLongitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
mapMyLocation.setRegion(region, animated: true)
解决方案
试试下面的代码
if let currentLatitude = locationManager.location?.coordinate.latitude,
let currentLongitude = locationManager.location?.coordinate.longitude {
//Map settings
mapMyLocation.showsUserLocation = true
mapMyLocation.delegate = self
let locationcoordinates = CLLocationCoordinate2D(latitude: currentLatitude, longitude: currentLongitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
mapMyLocation.setRegion(region, animated: true)
}
推荐阅读
- python - 如何通过 python sdk 下载传递内容(视频)?
- javascript - 在上传到谷歌云存储之前用锐利调整图像大小
- c# - 无法通过 pl/sql 在 Oracle 中的请求得到答案
- openstreetmap - 使用 QGIS 或 Maperitive 在 Openstreet Map 上生成 XYZ Tiles 获取坏图像
- azure - Azure B2C 自定义策略 - 有没有办法在 ClaimsProviderSelection 编排步骤中显示声明
- java - Apache Geode JSON PDX 问题
- c++ - 在 C++ 中生成具有相等概率的随机数
- angular - 是否有用于检测未使用的类属性的 eslint 规则?
- sql - 如何在 azure devops 发布管道中包含更新查询
- javascript - 如何测试中间件中返回的请求值