如何在MGLPolyline中找到最后一个坐标?

时间:2019-02-17 18:59:47

标签: ios arrays swift mapbox

我有一些代码可以在MGLPolyline中找到第一个和最后一个坐标。当前,我能够找到第一个和中间值,但无法找到最后一个值。贝娄是我的代码。在其中,您可以看到如何找到第一个和中间坐标。

var arrayOfCoordinates = [CLLocationCoordinate2D]()
var arrayOfPolylines = [MGLPolyline]()


 for value in arrayOfPolylines {
        mapView.addAnnotation(value)

        //This is for the first and last snakehead and tail stuff
        let lastLoc = value.coordinate.latitude
        let newLoc = value.coordinate.longitude

        let firstCor = value.coordinates[0].latitude
        let secondCor = value.coordinates[0].longitude
       ...

我尝试了以下方法来找到值:

value.coordinates[value.coordinates.count] //This does not compile as it says 
  

类型'UnsafeMutablePointer'的值没有成员'count'

所以我的问题是如何找到最后一个值(坐标)?

1 个答案:

答案 0 :(得分:2)

好吧,您的代码对此不起作用。使用当前代码,您将无法轻松找到最后一个值,您必须执行数学过程以确定坐标数,然后检查for循环是否经过了足够的时间才能到达最后一个坐标。

但是更简单的方法是创建CLLocation数组,然后可以使用以下代码进行访问:

            let lastLoc = self.arrayOfLocationList.last //last value here
            let newLoc = self.arrayOfLocationList.first