Graphics2D 在地图上画一条线
我正在尝试使用坐标在地图上绘制路径。我正在尝试使用 GeneralPath,但它不会创建一条线,只是在纬度/经度坐标所在的位置创建一堆点。我如何连接它们或者我可以使用其他东西吗?对 Graphics2D 不太熟悉..
region.add(new GeoPosition(47.2971, -122.3822));
region.add(new GeoPosition(47.2975, -122.3701));
region.add(new GeoPosition(47.3006, -122.3535));
region.add(new GeoPosition(47.2899, -122.3356));
region.add(new GeoPosition(47.2895, -122.3111));
region.add(new GeoPosition(47.2903, -122.2989));
region.add(new GeoPosition(47.2929, -122.2921));
region.add(new GeoPosition(47.2914, -122.2920));
region.add(new GeoPosition(47.2934, -122.2883));
Painter<JXMapViewer> overlay = new Painter<JXMapViewer>() {
public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
g = (Graphics2D) g.create();
//convert from viewport to world bitmap
Rectangle rect = map.getViewportBounds();
g.translate(-rect.x, -rect.y);
GeneralPath path = new GeneralPath();
//Polygon poly = new Polygon();
for(GeoPosition gp : region) {
//convert geo to world bitmap pixel
mapViewer.setZoom(60);
Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
//poly.addPoint((int)pt.getX(),(int)pt.getY());
path.moveTo((int)pt.getX(),(int)pt.getY());
path.lineTo((int)pt.getX(),(int)pt.getY());
}
//do the drawing
g.setColor(new Color(255,0,0,100));
g.fill(path);
g.setColor(Color.RED);
g.draw(path);
g.dispose();
}
I'm trying to paint a path on a map using coordinates. I'm trying to use GeneralPath but it doesn't create a line just bunch of dots where lat/long coordinates are. How do I connect them or is there something else that I can use? Not really familiar with Graphics2D..
region.add(new GeoPosition(47.2971, -122.3822));
region.add(new GeoPosition(47.2975, -122.3701));
region.add(new GeoPosition(47.3006, -122.3535));
region.add(new GeoPosition(47.2899, -122.3356));
region.add(new GeoPosition(47.2895, -122.3111));
region.add(new GeoPosition(47.2903, -122.2989));
region.add(new GeoPosition(47.2929, -122.2921));
region.add(new GeoPosition(47.2914, -122.2920));
region.add(new GeoPosition(47.2934, -122.2883));
Painter<JXMapViewer> overlay = new Painter<JXMapViewer>() {
public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
g = (Graphics2D) g.create();
//convert from viewport to world bitmap
Rectangle rect = map.getViewportBounds();
g.translate(-rect.x, -rect.y);
GeneralPath path = new GeneralPath();
//Polygon poly = new Polygon();
for(GeoPosition gp : region) {
//convert geo to world bitmap pixel
mapViewer.setZoom(60);
Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
//poly.addPoint((int)pt.getX(),(int)pt.getY());
path.moveTo((int)pt.getX(),(int)pt.getY());
path.lineTo((int)pt.getX(),(int)pt.getY());
}
//do the drawing
g.setColor(new Color(255,0,0,100));
g.fill(path);
g.setColor(Color.RED);
g.draw(path);
g.dispose();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您只需要一个 moveTo(...) 方法即可开始绘制线条。然后您执行多个 lineTo(...) 方法来绘制实际的线条。
这是我很久以前在网上找到的一个例子:
I believe you need only a single moveTo(...) method to start the drawing of the line. Then you do multiple lineTo(...) method to draw the actual line.
Here is an example I found on the web a long time ago: