2016-04-30 26 views
1

Google maps API可以在地图上创建包含连接点在一起的多段线的图层。在胶子中创建多段线mapLayer

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

请指点

回答

3

虽然没有了上MapView的顶部绘制直线,折线或多边形没有明确的API,该MapLayer在这里你可以得出任何的JavaFX Shape,提供你把它扩展到照顾一层地图坐标。

对于这一点,如果你有一个看看PoiLayerclass,你可以看到,对于任何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()); 
     } 
    } 
} 

现在,在试听课,打造集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

+0

谢谢你,优秀的答案和可行的例子:) – Ron