我一直在为我的应用程序使用 Mapbox 来生成路线和逐向导航,并且运行良好。但是我想避免通过路线的某些坐标,但我无法弄清楚。
获取路线的代码:
Directions.shared.calculate(options) { [unowned self] (waypoints, routes, error) in
// Take first route and customize it in a way to get around some coordinates
}
这是一个场景:
1- 用户位置是 纬度 = 37.332331410000002,经度 = -122.0312186
2- 用户要去 Santa Clara Unified School
位于 latitude = 37.354100000000003,longitude = -121.9552
3- Api 生成以下路由:
[0] = {
latitude = 37.332329999999999
longitude = -122.03118000000001
}
[1] = {
latitude = 37.332619999999999
longitude = -122.03118000000001
}
[2] = {
latitude = 37.332609999999995
longitude = -122.03097000000001
}
[3] = {
latitude = 37.332609999999995
longitude = -122.03076000000001
}
[4] = {
latitude = 37.332199999999993
longitude = -122.03076000000001
}
[5] = {
latitude = 37.331689999999995
longitude = -122.03076000000001
}
[6] = {
latitude = 37.331689999999995
longitude = -122.03190000000002
}
[7] = {
latitude = 37.331719999999997
longitude = -122.03199000000002
}
[8] = {
latitude = 37.331759999999996
longitude = -122.03205000000003
} ...
4-假设生成的路线经过 East Homestead Rd,我希望能够避开这条路并生成一条新路线,即使它是一条更长的路线。在下面的屏幕中避开红色路线,因为经过 East Homestead Rd 并选择不经过 East Homestead Rd 的下一条最快路线
如有任何帮助,我们将不胜感激!
编辑:这是查找 route 是否有要避开的点的查询
// $linestring is the array of coordinates from the route in the string format of (lng lat,lng2 lat2,lng3 lat3,lng4 lat4....)
$query = $this->em->createQuery('
SELECT count(i) as counter
FROM HitsBundle:Hit i
WHERE i.datetime BETWEEN :lastMonth AND :now
AND
MBRCovers(
ST_Buffer(
ST_GeomFromText(\'LineString('.$linestring.')\') ,
0.00001
),
i.coordinates
) = 1
GROUP BY i.coordinates
HAVING counter > 1
')
->setParameter('lastMonth', $lastMonth)
->setParameter('now', new \DateTime())
->setMaxResults(1);
最佳答案
我在这里可能是粗略的猜测,但是通过 Mapbox API 查看它在生成路由时没有任何可以避免的选项,因此您需要在客户端实现一些路由选择逻辑。
基本上,您需要有一个算法来获取一组要避免的点,并检查您的路线几何 GeoJSON 或折线是否在给定点的某个阈值范围内。如果是 - 丢弃路由(或降低路由优先级)。
当然,如果 Mapbox 提供的所有路线都被丢弃,它可能找不到路线 - Mapbox 不知道您的限制,因此对路线使用权重可能是解决此问题的一种选择。
这些帖子可能会给你一些提示:
关于ios - 使用 Mapbox Direction API 生成路线时避免坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50659938/