在Swift中将坐标插入数组时出错

问题描述 投票:0回答:1

无法将'[Dictionary]'类型的值转换为预期的参数类型'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)


        })
    }

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

我究竟做错了什么?如何正确插入坐标?我已经阅读了有关此内容的其他主题,但其中没有一个实际上正在运行。所以我要创建一个新的。

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

ios swift annotations polyline cllocation
1个回答
1
投票

currentLatcurrentLong变量是String,阵列类型是CLLocationCoordinate2D

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

此外,我的建议 - 使用guard来保存应用程序崩溃的情况,其中self.locationManager.location == nil

所以,这是一个例子:

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

© www.soinside.com 2019 - 2024. All rights reserved.