GMSMutablePath remove/replace 坐标问题,折线
GMSMutablePath remove/replace coordinate issue, polyline
我正在构建一个应用程序,它使用 Google 地图方向 API 给我一条路线折线,以便我可以在我的 mapView 上向用户显示。有点像 Google 地图逐向服务,但在我的应用程序中。
这张地图上的标记是 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))
}
}
}
我感觉它正在跳过删除,因为
中的 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
它在做什么,让它跳过第二个 waypoint/coordinate?
PS: 当我调用 removeCoordinateAtIndex(0) 时,是否所有内容都向下滑动到一个位置?例如,如果删除前的路径是:[0, 1, 2, 3],删除后的路径将是:[1, 2, 3](如此调整大小),或者实际上 [ , 1, 2, 3] 与索引 0 处为空白?
找到问题了!
行
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 将始终只与路径中的下一个坐标进行比较。
我正在构建一个应用程序,它使用 Google 地图方向 API 给我一条路线折线,以便我可以在我的 mapView 上向用户显示。有点像 Google 地图逐向服务,但在我的应用程序中。
这张地图上的标记是 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))
}
}
}
我感觉它正在跳过删除,因为
中的 0path.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
它在做什么,让它跳过第二个 waypoint/coordinate?
PS: 当我调用 removeCoordinateAtIndex(0) 时,是否所有内容都向下滑动到一个位置?例如,如果删除前的路径是:[0, 1, 2, 3],删除后的路径将是:[1, 2, 3](如此调整大小),或者实际上 [ , 1, 2, 3] 与索引 0 处为空白?
找到问题了!
行
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 将始终只与路径中的下一个坐标进行比较。