我想在生成路线时阻挡某些道路并避开它。
我正在使用Graphhopper basic map sample
我找到了这段代码 Weighting 。我相信这就是我正在寻找的功能,但我没有运气集成它。
我真的很感谢任何帮助我展示如何将两个代码组合在一起的帮助。非常感谢示例代码。
提前致谢。
最佳答案
所以,我将分享我的代码示例,其中展示了我如何实现自定义权重,但它们类似于 example你提到过。
首先,您必须扩展类GraphHopper
并重写方法createWeighting(WeightingMap WeightingMap, FlagEncoder编码器)
。
public class MyGraphHopper extends GraphHopper {
@Override
public Weighting createWeighting(WeightingMap weightingMap, FlagEncoder encoder) {
String weighting = weightingMap.getWeighting();
if (Consts.CURRENT_TRAFFIC.equalsIgnoreCase(weighting)) {
return new CurrentTrafficWeighting(encoder);
} else {
return super.createWeighting(weightingMap, encoder);
}
}
}
然后,您实现自定义权重,在您的情况下,您实现阻塞某些道路的逻辑。
public class CurrentTrafficWeighting extends AbstractWeighting {
protected final static double SPEED_CONV = 3.6;
public CurrentTrafficWeighting(FlagEncoder encoder) {
super(encoder);
System.out.println("Current traffic weighting");
}
@Override
public double getMinWeight(double distance) {
return distance;
}
@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
double speed = reverse ? flagEncoder.getReverseSpeed(edgeState.getFlags()) : flagEncoder.getSpeed(edgeState.getFlags());
if (speed == 0)
return Double.POSITIVE_INFINITY;
double time = edgeState.getDistance() / speed * SPEED_CONV;
return time;
}
@Override
public String getName() {
return Consts.CURRENT_TRAFFIC;
}
}
现在,也许对您来说最重要的部分是,它展示了如何连接这两段代码。当您创建新请求时,您必须将权重设置为自定义权重(您实现的权重)。这样,在计算最佳路线时,将使用您自定义的权重(某些道路受阻)。
public static void main(String[] args) {
MyGraphHopper hopper = new MyGraphHopper();
hopper.setOSMFile(OSM_FILE_PATH);
hopper.setGraphHopperLocation(GRAPH_FOLDER);
hopper.clean();
hopper.setEncodingManager(new EncodingManager("car"));
hopper.setCHEnable(false);
hopper.importOrLoad();
GHRequest req = new GHRequest(startPoint.getX(), startPoint.getY(), finishPoint.getX(), finishPoint.getY())
.setWeighting(Consts.CURRENT_TRAFFIC)
.setVehicle("car")
.setLocale(Locale.US)
.setAlgorithm(AlgorithmOptions.DIJKSTRA_BI);
GHResponse rsp = hopper.route(req);
}
关于android - 在graphhopper中封锁某些道路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766089/