在胶子mapLayer中创建折线

2024-04-23

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

我搜索了在哪里可以找到 gluon 的 mapLayer 的示例或实现。

请指教


虽然没有明确的 API 用于在对象之上绘制直线、折线或多边形MapView, the MapLayer是一个可以在其中绘制任何 JavaFX 的层Shape,前提是您负责将其缩放到地图坐标。

为此,如果您查看PoiLayer class https://bitbucket.org/gluon-oss/gluon-maps/src/tip/src/main/java/com/gluonhq/maps/demo/PoiLayer.java,你可以看到对于任何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());
        }
    }
}

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

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;
}

您将获得一张地图,其顶部带有您的地理定位多边形。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在胶子mapLayer中创建折线 的相关文章

随机推荐