驾车路线规划
本页只规划汽车路线。步行使用步行路线规划;需要实时行进引导时使用驾车导航。
最小可接入示例
先按快速开始配置授权并初始化 MapsInitializer。以下类可放入 example/docs/DrivePlanningExample.java,在主线程创建和调用。回调中的 DrivePath 可用于更新自己的路线卡片。
package example.docs;
import android.content.Context;
import java.util.function.Consumer;
import cn.sukha.mapsdk.services.core.*;
import cn.sukha.mapsdk.services.route.*;
public final class DrivePlanningExample implements AutoCloseable {
private final RouteSearch search;
private boolean closed;
public DrivePlanningExample(Context context) { search = new RouteSearch(context); }
public void plan(LatLonPoint from, LatLonPoint to,
Consumer<DrivePath> success, Consumer<SdkException> failure,
Runnable noRoute) {
search.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
@Override public void onDriveRouteSearched(DriveRouteResult result, int code) {
if (closed) return;
if (code != SdkException.SUCCESS) { failure.accept(search.getLastError()); return; }
if (result == null || result.getPaths().isEmpty()) { noRoute.run(); return; }
success.accept(result.getPaths().get(0));
}
});
search.calculateDriveRouteAsyn(new RouteSearch.DriveRouteQuery(
new RouteSearch.FromAndTo(from, to), RouteSearch.DRIVING_TIME));
}
public void cancel() { search.cancel(); }
@Override public void close() { closed = true; search.destroy(); }
}
起终点由业务选择或设备定位提供,例如 new LatLonPoint(21.67623, 100.02228)。成功后读取 path.getDistance()、path.getDuration()、path.getPolyline() 和 path.getSteps()。页面最终销毁时调用本示例的 close()。
策略与高级参数
| 参数 | 范围与行为 |
|---|---|
DRIVING_TIME |
时间优先,常量值 0 |
DRIVING_DISTANCE |
距离优先,常量值 1;仍遵守车辆通行规则 |
passedByPoints |
最多 16 个途经点,依次经过 |
avoidpolygons |
最多 32 个区域,每个 3–16 个顶点 |
avoidRoad |
最多 200 字符;非空时本次请求优先使用道路避让,不同时发送区域避让 |
高级构造器为 DriveRouteQuery(endpoints, mode, passedByPoints, avoidpolygons, avoidRoad)。不使用的可选项传 null。服务端还必须支持相应约束;参数校验通过不等于服务已经支持全部选项。
将结果画到地图
把每个 LatLonPoint 按纬度、经度转换为 cn.sukha.mapsdk.maps.model.LatLng,传入 PolylineOptions.addAll(...) 后通过 SukhaMap.addPolyline(...) 绘制。保存返回的 Polyline,新结果到来时更新已有线或移除旧线,避免每次查询累积覆盖物。地图生命周期见创建地图。
路线预览不会启动导航、采集位置或播报。不要将 DrivePath 直接传给不存在的 startNavi(path);导航页和控制器按各自流程准备导航路线。