【发布时间】:2016-07-15 03:20:31
【问题描述】:
我正在构建一个应用程序,它使用 Google Maps Directions API 为我提供一条路线折线,以便我可以在我的 mapView 上向用户显示。有点像 Google 地图的转向服务,但在我的应用程序中。
这张地图上的标记是谷歌路线给出的折线坐标
以上所有坐标(标记)都存储在变量“path”中,它是一个 GMSMutablePath。每当用户的位置靠近路径上的坐标时(按顺序移动,首先坐标 0,然后是 1..)我通过 removeCoordinateAtIndex() 从路径中删除该坐标,以便折线消失(例如:从标记 0 到标记 1) 从我的地图。目的是我希望折线始终从用户的位置开始显示,而不是我的路线的起点。
问题: 但是,我不确定为什么,但是导致 removeCoordinateAtIndex() 的 if 语句似乎只在我在路径(我的 GMSMutablePath)上的索引 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))
}
}
}
我感觉它正在跳过删除,因为
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) 时,所有东西都会滑回一个位置吗?例如,如果remove之前的路径是:[0,1,2,3],那么remove之后的路径是:[1,2,3](如此调整大小),还是实际上是[ , 1, 2, 3]索引 0 处的空白?
【问题讨论】:
-
什么是 pathIndex ?
标签: ios swift google-maps google-maps-api-3 google-maps-sdk-ios