如何在地图上绘制多个圆形和矩形



对于我所在大学的一个项目,我需要在地图中显示芝加哥的所有十字路口和一些车站,我已经有了带数据的LinkedLists,我需要用十字路口的位置画圆圈,用车站的位置画矩形。我使用的是jxMaps库,基于这些示例,我能够根据开发人员提供的示例绘制一个圆形和一个矩形来测试metods,但如果我在打开地图时试图用循环绘制多个,它将保持灰色。这是我的代码:

public class Draw extends MapView
{
private static final long serialVersionUID = 1L;
Map map;
IList <Integer, Intersetion> intersections;
IList <Integer, Station> stations;
public Draw(MapViewOptions options, IList <Integer, Intersection> inter, IList <Integer, Station> est)
{
super(options);
// Setting of a ready handler to MapView object. onMapReady will be called when map initialization is done and
// the map object is ready to use. Current implementation of onMapReady customizes the map object.
setOnMapReadyHandler(new MapReadyHandler()
{
@Override
public void onMapReady(MapStatus status)
{
// Check if the map is loaded correctly
if (status == MapStatus.MAP_STATUS_OK)
{
map = getMap();
intersections = inter; // I Load the list with the intersections data
stations = est; // I load the list with the stations data
rectangle();
circle();
// Creating a map options object
MapOptions mapOptions = new MapOptions();
// Creating a map type control options object
MapTypeControlOptions controlOptions = new MapTypeControlOptions();
// Changing position of the map type control
controlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting map type control options
mapOptions.setMapTypeControlOptions(controlOptions);
// Setting map options
map.setOptions(mapOptions);
// Setting the map center
map.setCenter(new LatLng(41.875486, -87.626570));
// Setting initial zoom value
map.setZoom(9.0);
}
}
});
}
public void circle ()
{
CircleOptions options = new CircleOptions();
options.setFillOpacity(0);
options.setStrokeColor("#CB4335");
options.setStrokeWeight(5.0);
for (Intersetion inter: intersections)
{
Circle circle = new Circle(map);
circle.setCenter(new LatLng(inter.darLatitude(), inter.darLongitude()));
circle.setRadius(50);
circle.setOptions(options);
}
}
public void rectangle()
{
RectangleOptions options = new RectangleOptions();
options.setFillOpacity(0);
options.setStrokeColor("#2E86C1");
int i = 0;
for (Station rect: stations)
{
Rectangle rectangulo = new Rectangle (map);
LatLngBounds bounds = new LatLngBounds (new LatLng (rect.darLatitude() - 0.0004, rect.darLongitude() - 0.0006), new LatLng (rect.darLatitude() + 0.0004, rect.darLongitude() + 0.0006));
rectangle.setBounds(bounds);
rectangle.setOptions(optionts);
}
}
}

我分析了提供的源代码,除了设置笔划颜色的地方之外,它看起来还不错。你必须在HTML格式中使用颜色,所以你必须更改:

options.setStrokeColor(Color.RED.toString()); to options.setStrokeColor("#FF0000");

然而,这并不是屏幕变灰的原因。当设置地图属性(inside onMapReady() handler(时出现问题时,通常会出现灰色屏幕。

因此,你必须检查是否发生了任何异常,如果发生了,则修复其根本原因。

此外,您还可以启用日志记录并检查它是否有任何错误。您可以将-Djxmaps.logging.level=ALL参数添加到应用程序的VM选项中。

编辑________________________________________________________________________

下面是一个允许创建多个圆的代码示例:

map.addEventListener("click", new MapMouseEvent() {
@Override
public void onEvent(MouseEvent mouseEvent) {
final Circle circle = new Circle(map);
circle.setRadius(2000);
circle.setCenter(mouseEvent.latLng());
}
});

实际上,由于某种原因,如果我在设置了映射的选项后在末尾调用方法circle和rectangle,这有点奇怪,因为当我只按照问题帖子中出现的顺序创建一个圆形或一个矩形时,它工作得很好。

最新更新