【问题标题】:Swift 3 - Show coordinates on mapSwift 3 - 在地图上显示坐标
【发布时间】:2017-03-17 00:51:51
【问题描述】:

我需要帮助。我正在尝试在地图上显示我的坐标。我正在使用 MVC 模型构建我的项目。但我确实收到一个错误,说我不能使用 CLLocationDegrees,我必须改用 CLLocatiocoordinates2D。请参阅下面的代码。

编辑:更新了我的代码,现在它可以工作了!感谢您的回答!

这里是我保存信息的地方(如果你可以这么说?)

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

}()

【问题讨论】:

  • 你在哪一行得到了错误?
  • 请显示给出错误的确切代码。
  • 我已经更新了代码。

标签: swift xcode location coordinates


【解决方案1】:

你的经纬度应该是一种

CLLocationCoordinate2D

不是

CLLocationDegrees

否则您只能定义一个 CLLocationCoordinate2D 类型的变量。

喜欢

var location = CLLocationCoordinate2D() 

location.coordinate.latitude = "-72.455412"
location.coordinate.longitude = "40.005165"

您可以通过同样的方式检索经纬度。只需更改您的模型变量。我想应该没问题。

您的代码中的其他内容看起来不错。

【讨论】:

  • 是的,我已经弄清楚了,但我需要它们是 CLLocationDegrees 否则我会在我写“名称”、“纬度”和“经度”的模型中出现错误。跨度>
  • 只需将模型 lat,long 更改为 CLLocationCoordinate2D 应该没有任何问题。为什么需要 CLLocationDegrees?
  • 好的,抱歉,我对此有点陌生。但是,如果我将模型纬度和经度更改为 CLLCoordinate2D,错误会消失。但现在我是否在我的let sebbeUser = User(name: "Sebbe", latitude: 37.33233141, longitude: -122.0312186) 中收到此错误,而不是“无法将 'Double' 类型的值转换为预期的参数类型 'CLLocationCoordinate2D'”
  • 试试这个语法:CLLocationCoordinate2D(latitude: Double Lat, longitude: Double Long) 例子:CLLocationCoordinate2D(latitude: 10.30, longitude: 44.34)
  • 我已将模型更新为 CLCoordinates2D。但是我应该在哪里写 CLLocationCoordinate2D(latitude: Double Lat, longitude: Double Long)?
【解决方案2】:

您需要将它们放入 struct CLLocatiocoordinates2D 中。

CLLocatiocoordinates2D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多