我正在使用展开的地图来创建标记。当用户将鼠标悬停在标记上时,会显示标题。但是其他标记与标题重叠,因此它会隐藏标题中显示的信息。为了防止这种情况,我使用PGraphics将标题绘制为:
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);
}
它解决了问题,现在标题没有与标记重叠,但它没有在标记下面绘制,而是在屏幕上随机绘制一些,如图所示 我该如何避免它?请帮忙。
答案 0 :(得分:1)
您需要在绘制之前存储缓冲区的位置。基本上,您需要更改此行:
image(buffer, 0,0);
对于这样的事情:
image(buffer, bufferX, bufferY);
根据您希望缓冲区显示的位置,您必须计算bufferX
和bufferY
。一个非常愚蠢的实现可能是这样的:
public void showTitle(PGraphics pg, float x, float y)
{
bufferX = x;
bufferY = y;
//rest of your code
我对展开地图不够熟悉,无法告诉您该做什么,但这些都是基础知识。请注意,展开地图可能正在执行自己的翻译,因此您可能必须使用pushMatrix()
和popMatrix()
功能。另请注意,您可能需要从地图空间投影到屏幕空间。展开地图库可能包含有关如何执行此操作的文档。
答案 1 :(得分:0)
在设置中,您创建了具有一定大小和偏移量的地图。缓冲区应创建为相同大小。
但是最重要的是绘制具有相同偏移量的缓冲区。
您的标题只是以负偏移绘制,而不是随机的。
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);
}
答案 2 :(得分:0)
我参加了同一门Coursera课程(Java中的面向对象编程),并且还致力于修复此功能。这是我解决此问题的方法。
我意识到标题被覆盖了,因为标记是在EarthquakeCityMap类的draw()部分的map.draw()方法内一遍又一遍地绘制的。因此,解决方法是将showTitle实现从标记类移动到EarthquakeCityMap类,并在标记的屏幕位置而不是纬度和经度绘制标题。
UnfoldingMaps库中的SimplePointMarker类具有从AbstractMarker类继承的getScreenPosition()方法。有关更多详细信息,请参见JavaDoc。由于CommonMarker类是SimplePointMarker类的子类,而EarthquakeMarker类是CommonMarker的子类,因此EarthquakeMarker类也具有默认可用的getScreenPosition()方法。
下面是在EarthquakeCityMap.java文件中显示地震标题的方法。请注意,由于EarthquakeCityMap类扩展了PApplet,因此您可以在此处自动使用Processing的图形方法。
/** 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);
}
}
}
最后,这是显示标题不再被标记覆盖的结果图像。霍雷〜 The earthquake map display when the mouse hovers one of the earthquake marker