在gluon mapLayer中创建折线 [英] create a polyline in gluon mapLayer
问题描述
谷歌地图API可以在地图上创建一个包含折线连接点的图层。
我已经搜索了我可以找到示例的位置或者为胶子的mapLayer实现这个。
请建议
在 MapView
之上没有用于绘制线条,折线或多边形的明确API, MapLayer
是您的一个图层可以绘制任何JavaFX Shape
,只需将其缩放到地图坐标即可。
为此,如果你看看 PoiLayer
Google maps API makes it possible to create a layer on the map containing a polyline linking points together.
I have searched where I could to find an example or an implementation for this for gluon's mapLayer.
Please advice
While there's no explicit API for drawing lines, polylines or polygons on top of a MapView
, the MapLayer
is a layer where you can draw any JavaFX Shape
, providing you take care of scaling it to the map coordinates.
For that, if you have a look at the PoiLayer
class, you can see that for any MapPoint
(defined by latitude and longitude) you can get a 2D point (defined by x and y), and you can draw a node at that position:
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());
So if you want to create, for instance, a Polygon
based on a set of points, you have to add a Polygon
object to the layer:
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());
}
}
}
Now, on the demo class, create a set of mapPoints, and add them to the map:
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;
}
And you will have a map with your geo-located polygon on top of it.
这篇关于在gluon mapLayer中创建折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!