Java 在展开地图时如何避免标记标题重叠?
我正在使用展开贴图创建标记。当用户将鼠标悬停在标记上时,将显示标题。但其他标记与标题重叠,因此隐藏了标题中显示的信息。为了避免这种情况,我使用PGraphics将标题绘制为:Java 在展开地图时如何避免标记标题重叠?,java,maps,processing,Java,Maps,Processing,我正在使用展开贴图创建标记。当用户将鼠标悬停在标记上时,将显示标题。但其他标记与标题重叠,因此隐藏了标题中显示的信息。为了避免这种情况,我使用PGraphics将标题绘制为: public void showTitle(PGraphics pg, float x, float y) { String name = getCity() + " " + getCountry() + " "; String pop = "Pop: " + getPopulation() + "
public void showTitle(PGraphics pg, float x, float y)
{
String name = getCity() + " " + getCountry() + " ";
String pop = "Pop: " + getPopulation() + " Million";
/
/buffer is defined already.
buffer.beginDraw();
buffer.fill(255, 255, 255);
buffer.textSize(12);
buffer.rectMode(PConstants.CORNER);
buffer.rect(x, y-TRI_SIZE-39, Math.max(pg.textWidth(name), pg.textWidth(pop)) + 6, 39);
buffer.fill(0, 0, 0);
buffer.textAlign(PConstants.LEFT, PConstants.TOP);
buffer.text(name, x+3, y-TRI_SIZE-33);
buffer.text(pop, x+3, y - TRI_SIZE -18);
buffer.endDraw();
}
然后通过该方法在屏幕上绘制缓冲区
public void draw() {
map.draw();
image(buffer, 0,0);
}
它解决了这个问题,现在标题没有与标记重叠,但它不是在标记的正下方绘制的,而是在屏幕上随机绘制的,如图所示
我怎样才能避免呢?请提供帮助。在绘制缓冲区之前,您需要存储缓冲区的位置。基本上,您需要更改这一行:
image(buffer, 0,0);
对这样的事情:
image(buffer, bufferX, bufferY);
public void showTitle(PGraphics pg, float x, float y)
{
bufferX = x;
bufferY = y;
//rest of your code
您必须根据希望缓冲区显示的位置计算bufferX
和bufferY
。一个非常愚蠢的实现可能是这样的:
image(buffer, bufferX, bufferY);
public void showTitle(PGraphics pg, float x, float y)
{
bufferX = x;
bufferY = y;
//rest of your code
我对展开地图不太熟悉,无法准确地告诉您该做什么,但这些都是基础。请注意,展开贴图可能需要自己进行转换,因此您可能必须使用
pushMatrix()
和popMatrix()
函数。还请注意,您可能需要从地图空间投影到屏幕空间。展开贴图库中可能有关于如何执行此操作的文档。在设置中,您创建了具有一定大小和偏移量的贴图。应以相同的大小创建缓冲区
但最重要的是绘制具有相同偏移量的缓冲区
标题只是用负偏移量绘制的,而不是随机绘制的
private PGraphics buffer;
public void setup() {
size(900, 700);
//Create map with offset
map = new UnfoldingMap(this, 200, 50, 650 ,600, new Google.GoogleMapProvider());
//Create buffer, same size as map
buffer = createGraphics(650, 600);
//rest of the code..
}
public void draw() {
background(0);
map.draw();
//put buffer over the map, with same offset
image(buffer, 200, 50);
}
我参加了同样的课程(Java中的面向对象编程),并致力于修复此功能。下面是我如何解决这个问题的 我意识到标题被覆盖是因为标记在EarthquakeCityMap类的draw()部分的map.draw()方法中被反复绘制。因此,解决方案是将showTitle实现从marker类移动到EarthquakeCityMap类,并将标题绘制在标记的屏幕位置,而不是纬度和经度 UnfolingMaps库中的SimplePointMarker类具有从AbstractMarker类继承的方法getScreenPosition()。有关更多详细信息,请参阅JavaDoc。因为CommonMarker类是SimplePointMarker类的子类,而EarthquakeMarker类是CommonMarker的子类,所以EarthquakeMarker类的getScreenPosition()方法也默认可用 下面是在EarthquakeCityMap.java文件中显示地震标题的方法。请注意,您可以在此处自动使用Processing的图形方法,因为EarthquakeCityMap类扩展了PApplet
/** Show the title of the earthquake if this marker is selected */
private void showEqTitle(EarthquakeMarker m, UnfoldingMap map)
{
float x = m.getScreenPosition(map).x;
float y = m.getScreenPosition(map).y;
String title = m.getTitle();
pushStyle();
rectMode(PConstants.CORNER);
stroke(110);
fill(255,255,255);
rect(x, y + 15, textWidth(title) + 6, 18, 5);
textAlign(PConstants.LEFT, PConstants.TOP);
fill(0);
text(title, x + 3 , y +18);
popStyle();
}
下面是我如何在draw()函数中实现上面的方法
public void draw() {
background(0);
map.draw();
addKey();
for (Marker m: quakeMarkers) {
EarthquakeMarker eq = (EarthquakeMarker) m;
if (m.isSelected()) {
showEqTitle(eq, map);
}
}
}
最后,这里是显示标题不再被标记覆盖的结果图像。霍雷~
请提供一份详细的报告。你可以使用简化的硬编码示例,而不是整个草图。我的做法与你提到的几乎相同。我存储了所选标记的屏幕位置,并在image()函数中使用了这些位置,但它不是这样工作的。有没有其他解决方案可以“聚焦”当前组件?@BatulMajid我真的不知道你说的聚焦是什么意思。如果您有后续问题,请在新问题中发布更新,我们将从那里开始。