在 gluon mapLayer 中创建折线
create a polyline in gluon mapLayer
Google 地图 API 可以在地图上创建一个包含将点连接在一起的折线的图层。
我搜索了在哪里可以找到 gluon 的 mapLayer 的示例或实现。
请指教
虽然没有明确的 API 用于在 MapView
之上绘制线条、折线或多边形,但 MapLayer
是一个可以在其中绘制任何 JavaFX 的层 Shape
,前提是您负责将其缩放到地图坐标。
为此,如果您查看 PoiLayer
class,您会发现对于任何 MapPoint
(由纬度和经度定义),您可以获得一个二维点(由 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());
}
}
}
现在,在演示 class 上,创建一组地图点,并将它们添加到地图中:
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;
}
您将得到一张地图,上面有您的地理定位多边形。
Google 地图 API 可以在地图上创建一个包含将点连接在一起的折线的图层。
我搜索了在哪里可以找到 gluon 的 mapLayer 的示例或实现。
请指教
虽然没有明确的 API 用于在 MapView
之上绘制线条、折线或多边形,但 MapLayer
是一个可以在其中绘制任何 JavaFX 的层 Shape
,前提是您负责将其缩放到地图坐标。
为此,如果您查看 PoiLayer
class,您会发现对于任何 MapPoint
(由纬度和经度定义),您可以获得一个二维点(由 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());
}
}
}
现在,在演示 class 上,创建一组地图点,并将它们添加到地图中:
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;
}
您将得到一张地图,上面有您的地理定位多边形。