Swift 3-在地图上显示坐标
我需要帮助。我试图在地图上显示我的坐标。我正在使用MVC模型构建我的项目。但我确实得到一个错误,即我不能使用CLLocationDegrees,而必须使用CLLocationCoordinated。请看下面我的代码 编辑:更新了我的代码,现在就可以了!谢谢你的回答 这里是我保存信息的地方(如果你可以这么说的话?) 这是我的模型:Swift 3-在地图上显示坐标,swift,xcode,location,coordinates,Swift,Xcode,Location,Coordinates,我需要帮助。我试图在地图上显示我的坐标。我正在使用MVC模型构建我的项目。但我确实得到一个错误,即我不能使用CLLocationDegrees,而必须使用CLLocationCoordinated。请看下面我的代码 编辑:更新了我的代码,现在就可以了!谢谢你的回答 这里是我保存信息的地方(如果你可以这么说的话?) 这是我的模型: import LBTAComponents import CoreLocation class HomeDataSource: Datasource { let u
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
}()
您需要将它们放入struct cllocatiocordinated
你的纬度和经度应该是 CLLocationCoordinated2D 不是 位置度 或者,您只能定义CLLocationCoordinate2D类型的一个变量 像 和检索lat long的方法相同。只需更改模型变量。我想应该没问题
您的代码中的其他内容看起来很好。哪一行出现了错误?请显示给出错误的确切代码。我现在已经更新了代码。是的,我已经找到了,但我需要它们是CLLOCATIONDEGRONES,否则我的模型中会出现错误,我会在其中写入“名称”、“纬度”和“经度”。只需更改您的模型纬度,长时间进入CLLocationCoordinate2D应该不会有任何问题。你为什么需要CLLocationDegrees?好吧,对不起,我对这个有点陌生。但是,如果我将我的模型纬度和经度更改为CLLCoordinated2D,错误就会消失。但是现在我在我的
let sebbeUser=User(name:“Sebbe”,纬度:37.33233141,经度:-122.0312186)中得到了这个错误“无法将类型为'Double'的值转换为预期的参数类型'CLLocationCoordinate2D'”
试试这种方法语法:CLLocationCoordinate2D(纬度:双纬度,经度:双长)示例:CLLocationCoordinate2D(纬度:10.30,经度:44.34),我已将模型更新为CLCoordinate2D。但是我应该在哪里写CLLocationCoordinate2D(纬度:双纬度,经度:双长)?
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
}()
var location = CLLocationCoordinate2D()
location.coordinate.latitude = "-72.455412"
location.coordinate.longitude = "40.005165"