【问题标题】:Error when inserting coordinates into array in Swift在 Swift 中将坐标插入数组时出错
【发布时间】:2019-07-23 14:14:58
【问题描述】:

无法将“[字典]”类型的值转换为预期值 参数类型“UnsafePointer”

是我在尝试将坐标插入数组时遇到的错误吗?

这是我的代码:

var locations: [CLLocationCoordinate2D] = []

    //Updating location + polylines
    @objc func update()
    {
        Annotationtimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true, block: { (timerr) in

            let currentLat = self.locationManager.location?.coordinate.latitude
            let currentLong = self.locationManager.location?.coordinate.longitude


            let location = [(currentLat!), (currentLong!)]


            self.locations.append(location) //HERE IS THE ERROR
            print(self.locations)


     //Draw polyline on the map
     let aPolyLine = MKPolyline(coordinates: locations, count: locations.count)

     //Adding polyline to mapview
     self.mapView.addOverlay(aPolyLine)


        })
    }

我正在尝试将当前位置插入一个数组,当我按下一个按钮时,我想从头开始绘制折线,通过所有坐标。

我做错了什么?如何正确插入坐标?我已经阅读了有关此的其他线程,但它们都没有真正起作用。所以我想创建一个新的。

为什么不喜欢?至少你可以告诉我原因。

【问题讨论】:

  • 只需将 'let location = ...' 替换为 'let location = CLLocationCoordinate2D.init(latitude: currentLat!, longitude: currentLong!)' 。您的问题是您添加的是 CGFloats 数组而不是添加 cllocationcoordinate

标签: ios swift annotations polyline cllocation


【解决方案1】:

currentLatcurrentLong变量为String,数组类型为CLLocationCoordinate2D

你不需要提取currentLatcurrentLong,最好这样做:self.locations.append(self.locationManager.location?.coordinate)

另外,我的建议 - 在self.locationManager.location == nil 的情况下使用guard 来避免应用程序崩溃。

所以,这里有一个例子:

guard let currentLocation = self.locationManager.location else { return } // show error or something else self.locations.append(currentLocation)

【讨论】:

  • 数组的类型为CLLocationCoordinate2Dlocation 的属性类型为CLLocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2015-04-18
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多