Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios GMS表格路径删除/替换坐标问题,多段线_Ios_Swift_Google Maps_Google Maps Api 3_Google Maps Sdk Ios - Fatal编程技术网

Ios GMS表格路径删除/替换坐标问题,多段线

Ios GMS表格路径删除/替换坐标问题,多段线,ios,swift,google-maps,google-maps-api-3,google-maps-sdk-ios,Ios,Swift,Google Maps,Google Maps Api 3,Google Maps Sdk Ios,我正在构建一个应用程序,它使用谷歌地图方向API为我提供一条路线多段线,这样我就可以在地图视图上向用户显示。有点像谷歌地图的轮流服务,但在我的应用程序中 此地图上的标记是Google Directions提供的多段线的坐标 上述所有坐标(标记)都存储在变量“path”中,该变量为GMSMUTABLE path。每当用户的位置靠近路径上的某个坐标时(按顺序移动,因此先是坐标0,然后是1..),我都会通过removeCoordinateAtIndex()从路径中删除该坐标,以便多段线从地图中消失(

我正在构建一个应用程序,它使用谷歌地图方向API为我提供一条路线多段线,这样我就可以在地图视图上向用户显示。有点像谷歌地图的轮流服务,但在我的应用程序中

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

上述所有坐标(标记)都存储在变量“path”中,该变量为GMSMUTABLE path。每当用户的位置靠近路径上的某个坐标时(按顺序移动,因此先是坐标0,然后是1..),我都会通过removeCoordinateAtIndex()从路径中删除该坐标,以便多段线从地图中消失(例如:从标记0到标记1)。其目的是希望多段线始终从用户位置开始显示,而不是从路线的原点开始显示

问题: 但是,我不知道为什么,但是导致removeCoordinateAtIndex()的if语句似乎只在我位于索引0上时删除。。2.4(偶数)在路径(我的路径)上,所以跳过中间的一个

这是我的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))

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

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

是什么让它跳过了第二个航路点/坐标


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

发现了问题

线路

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

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

相反。请注意pathIndex->1的更改,因此现在distanceFromLocation将始终仅与路径中的下一个坐标进行比较。

什么是pathIndex?
let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(pathIndex)).latitude, longitude: path.coordinateAtIndex(UInt(pathIndex)).longitude)
let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(1)).latitude, longitude: path.coordinateAtIndex(UInt(1)).longitude)