我需要帮助。我试图在地图上显示我的坐标。我正在使用 MVC 模型构建我的项目。但是我确实收到一个错误,说我不能使用 CLLocationDegrees,我必须改用 CLLocationcoordinates2D。在下面查看我的代码。
编辑:更新了我的代码,现在它可以工作了!感谢您的回答!
这是我保存信息的地方(如果你能这么说?)
import Foundation
import MapKit
struct User {
let name: String
let position: CLLocationCoordinate2D
}
这是我的模型:
import LBTAComponents
import CoreLocation
class HomeDataSource: Datasource {
let users: [User] = {
let position = CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.0312186)
let sebbeUser = User(name: "Sebbe", position: position)
return [sebbeUser]
}()
override func cellClasses() -> [DatasourceCell.Type] {
return [UserCell.self]
}
override func item(_ indexPath: IndexPath) -> Any? {
return users[indexPath.item]
}
override func numberOfItems(_ section: Int) -> Int {
return users.count
}
}
这是我的手机,你可以看到我已经成功打印了名字。您可以看到我在哪里发表评论以查看我的问题。
import LBTAComponents
import MapKit
import CoreLocation
class UserCell: DatasourceCell, CLLocationManagerDelegate, MKMapViewDelegate {
let distanceSpan: Double = 500
override var datasourceItem: Any? {
didSet {
guard let user = datasourceItem as? User else { return }
nameLabel.text = user.name
MapView.setCenter(user.position, animated: false)
}
}
let nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 14)
return label
}()
let MapView: MKMapView = {
let map = MKMapView()
map.isZoomEnabled = false
map.isScrollEnabled = false
map.isUserInteractionEnabled = false
return map
}()
你的纬度和经度应该是一种
CLLocationCoordinate2D
不是
CLLocationDegrees
.
否则,您只能定义一个 CLLocationCoordinate2D 类型的变量。
喜欢
var location = CLLocationCoordinate2D()
location.coordinate.latitude = "-72.455412"
location.coordinate.longitude = "40.005165"
同样的方式你可以检索经纬度。只需更改您的模型变量。我想应该没问题。
您的代码中的其他内容看起来不错。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句