GMSMutablePath删除/替换坐标问题,折线

时间:2016-03-27 21:02:37

标签: ios swift google-maps google-maps-api-3 google-maps-sdk-ios

我正在构建一个使用Google Maps Directions API的应用程序来为我提供路线折线,以便我可以在我的mapView上向用户显示。有点像谷歌地图转弯服务,但在我的应用程序内。

此地图上的标记是Google Directions提供的折线的坐标

enter image description here

以上所有坐标(标记)都存储在变量" path"中,这是一个GMSMutablePath。每当用户的位置靠近路径上的坐标时(按顺序行进,首先是坐标0,然后是1 ..)我通过removeCoordinateAtIndex()从路径中删除该坐标,以便折线消失(例如:从我的地图中的标记0到标记1)。目的是我希望折线始终从用户的位置开始显示,而不是我的路线的原点。

问题: 但是,我不确定为什么但是导致removeCoordinateAtIndex()的if语句似乎只在我在路径上的索引0 .. 2 .. 4(偶数)上时才删除(我的GMSMutablePath),所以跳过一个介于两者之间。

这是我的locationManager功能

pathIndex = 0

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[locations.count - 1]

    /*
        For each GPS update, check location of user against next waypoint 
    on route. If distance is within 6 meters (18 feet), increment pathIndex 
    and now draw polyline from current location to NEXT waypoint (if there  
    is one), and then compare user location to NEXT waypoint again, etc.
    */

    //Replace polyline to start display from where you are
    path.replaceCoordinateAtIndex(UInt(0), withCoordinate: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude))
    polyline.path = path

    //Get distance from current to next waypoint in path
    let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(pathIndex)).latitude, longitude: path.coordinateAtIndex(UInt(pathIndex)).longitude) //Coordinate of next waypoint
    let locToWaypoint = location.distanceFromLocation(waypoint) //Returns distance in meters
    print("dist: ", locToWaypoint, ", index: ", pathIndex)

    //If closer than 6 meters, remove polyline of that segment from map
    if (locToWaypoint < 6) {
        //If not on last step
        if (pathIndex < Int(path.count()) - 1) {
            pathIndex++
            //Remove last path
            print("Removing: ", path.coordinateAtIndex(UInt(0)))
            path.removeCoordinateAtIndex(UInt(0))

        }
    }
}

我觉得由于

中的0而跳过删除
path.removeCoordinateAtIndex(Uint(0))
path.replaceCoordinateAtIndex(UInt(0), withCoordinate: ...)

但是在改变之后,我仍然遇到同样的问题,所以我认为这可能是另一回事。

这是调试日志。请注意,只有第一个和第三个调用会删除坐标,第二个调用会删除。当我将pathIndex启动为1时,它会更改为第二次和第四次调用(总是在其间跳过一次):

dist: 0.0 , index:  0       
Removing:  CLLocationCoordinate2D(latitude: 36.131648, longitude: -80.275542)  //First time, so is removed. This is the origin.
dist: 40.3950268964149 , index:  1     //Now showing dist to next waypoint

dist: 14.8659227375514 , index:  1  //Second time; NOT removed. Somehow it just skips the coordinate at the index and goes on to calculate the distance to the NEXT waypoint (thus 14.86)

dist: 1.34445248711346 , index:  1  
Removing:  CLLocationCoordinate2D(latitude: 36.131931, longitude: -80.2758)   //Third time, IS removed.
dist: 45.4634994640154 , index:  2     //Now showing dist to next waypoint

这是做什么让它跳过第二个航点/坐标?

PS:当我调用removeCoordinateAtIndex(0)时,所有内容都会向下滑动一个位置吗?例如,如果删除之前的路径是:[0,1,2,3],删除后的路径将是:[1,2,3](如此调整大小),或实际上[,1,2,3]索引0处的空白?

1 个答案:

答案 0 :(得分:0)

发现问题!

该行

bind

应该阅读:

let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(pathIndex)).latitude, longitude: path.coordinateAtIndex(UInt(pathIndex)).longitude)

相反。注意路径索引的变化 - &gt; 1,所以现在distanceFromLocation将始终只比较路径中的下一个坐标。