在gluon mapLayer中创建折线

时间:2016-04-30 08:29:01

标签: javafx javafx-8 gluon gluon-desktop

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

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

请咨询

1 个答案:

答案 0 :(得分:6)

虽然在MapView之上没有用于绘制线条,折线或多边形的明确API,但MapLayer是一个可以绘制任何JavaFX Shape的图层,让您注意将其缩放到地图坐标。

为此,如果您查看PoiLayer class,您可以看到,对于任何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());

因此,如果您想基于一组点创建Polygon,则必须在图层中添加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());
        }
    }
}

现在,在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;
}

你将拥有一张地图上有地理位置多边形的地图。

poi