在gluon mapLayer中创建折线

Ron*_*Ron 2 javafx javafx-8 gluon gluon-desktop

Google maps API可以在地图上创建一个包含折线连接点的图层.

我已经搜索过哪里可以为胶子的mapLayer找到一个例子或实现.

请指教

Jos*_*eda 6

虽然没有对的顶部绘制直线,折线或多边形没有明确的API MapView,将MapLayer是一个层,在那里你可以画任何的JavaFX Shape,提供你把它扩展到地图坐标的照顾.

对于这一点,如果你有一个看看PoiLayer ,你可以看到,对于任何MapPoint(经度和纬度定义的),你可以得到一个2D点(x和y所定义的),你可以画在该位置的节点:

MapPoint point = new MapPoint(37.396256,-121.953847);
Node icon = new Circle(5, Color.BLUE);
Point2D mapPoint = baseMap.getMapPoint(point.getLatitude(), point.getLongitude());
icon.setTranslateX(mapPoint.getX());
icon.setTranslateY(mapPoint.getY());
Run Code Online (Sandbox Code Playgroud)

因此,如果要创建Polygon基于一组点的a,则必须Polygon向该层添加一个对象:

public class PoiLayer extends MapLayer {

    private final Polygon polygon;

    public PoiLayer() {
        polygon = new Polygon();
        polygon.setStroke(Color.RED);
        polygon.setFill(Color.rgb(255, 0, 0, 0.5));
        this.getChildren().add(polygon);
    }

    @Override
    protected void layoutLayer() {
        polygon.getPoints().clear();
        for (Pair<MapPoint, Node> candidate : points) {
            MapPoint point = candidate.getKey();
            Node icon = candidate.getValue();
            Point2D mapPoint = baseMap.getMapPoint(point.getLatitude(), point.getLongitude());
            icon.setTranslateX(mapPoint.getX());
            icon.setTranslateY(mapPoint.getY());

            polygon.getPoints().addAll(mapPoint.getX(), mapPoint.getY());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在demo类上,创建一组mapPoints,并将它们添加到地图中:

private final List<MapPoint> polPoints = Arrays.asList(
        new MapPoint(37.887242, -122.178799), new MapPoint(37.738729, -121.921567),
        new MapPoint(37.441704, -121.921567), new MapPoint(37.293191, -122.178799),
        new MapPoint(37.441704, -122.436031), new MapPoint(37.738729, -122.436031));

private MapLayer myDemoLayer () {
    PoiLayer poi = new PoiLayer();
    for (MapPoint mapPoint : polPoints) {
        poi.addPoint(mapPoint, new Circle(5, Color.BLUE));
    }
    return poi;
}
Run Code Online (Sandbox Code Playgroud)

并且您将在地图上找到包含地理位置多边形的地图.

POI