1

我想为展开地图上的每个标记添加一个图像

已经尝试使用 Buffer 但我不确定我现在应该做什么。

我的主要课程

import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.marker.*;

UnfoldingMap map;


import java.util.Map;    // import java hashmaps

// declare country hashmaps
// key // value
HashMap<String, Country> countries = new HashMap<String, Country>();
//PGraphics buffer;
int countryNumber;
Button country;
public void setup() {
  size(800, 600, P2D);
  smooth();  

  //buffer = createGraphics(800, 600);
  map = new UnfoldingMap(this);
  map.setTweening(true);
  map.zoomToLevel(3);

  MapUtils.createDefaultEventDispatcher(this, map);

  String[] lines = loadStrings("test.csv");  //read file and save lines in a string array

  countryNumber = lines.length; // how many lines
  println(countryNumber);

  for (int i=0; i<countryNumber; i++) { //all csv lines

    String line = lines[i];   /// get data from line. Ex: "Portugal,10,91"

    String[] elements = split(line, ","); //separate line by ','

    String name = elements[0]; // get country name
    float lat = parseInt(elements[20]); // lat
    float lon = parseInt(elements[21]); // lon
    PImage flagIcon;

    flagIcon = loadImage(elements[22]); //get image flag name

    Country P = new Country(name, lat, lon, flagIcon); 


    Location mapMarker = new Location(lat,lon);
    // Create point markers for locations
    SimplePointMarker eachMapMarker = new 
    SimplePointMarker(mapMarker);

    // Add markers to the map
    map.addMarkers(eachMapMarker);
    paises.put(nome, P );
  }
}



public void draw() {

  background(255);

  map.draw();    // draw map
  //image(buffer, 200, 50);
  for (Map.Entry p : Countries.entrySet() ) {    // get countries hashmap
    Country country = (Country) p.getValue();
    country.drawInfo();
    country.drawCoor();
    country.drawIcon();
  }
}

我的国家课

//my country class

class Country { 

  String name;
  float lat;
  float lon;
  int posX, posY;
  PImage flagIcon;

  Pais (String n, float la, float lo, PImage ic) {
    name = n;
    lat = la;
    lon = lo;
    flagIcon = ic;
  }

  void drawIcon() {
    image(flagIcon,lat,lon,16,16);
  }

  void drawInfo() {
    Location mapLocal = new Location(lat, lon);

    ScreenPosition mapPos = map.getScreenPosition(mapLocal);

    float scale = map.getZoom();

    // if pointer is near a country
    boolean onOver = false;
    if (dist(mouseX, mouseY, mapPos.x, mapPos.y)<2*scale) {
      onOver = true;
    }



    if (onOver) {
      noStroke();
      //fill(255,255,255,255);
      //rect(mouseX+18, mouseY-20, 120, 50);
      fill(0);
      text(name + "\nLat: " + lat + "\nLon: " + lon , mouseX+25, mouseY-5);
    }
  }

  void drawCoor() {
   //println(coordinates);

    fill(0);
    text("coordinates: " + mouseX + " " + mouseY, 650,10); 
  }
}

图片链接存储在我的 CSV 中

这就是我得到的,我可以看到我的图像与我的纬度和经度不同步。谁能帮我一把?

4

1 回答 1

0

当您在地图上绘制国家图标时,对于 drawIcon() 方法中 image(PImage image, float x, float y) 函数中 x 和 y 的输入,您应该使用标记在地图上的位置而不是纬度和经度属性。

具体方法称为AbstractMarker的getScreenPosition(UnfoldingMap map)。有关详细信息,请参阅 javadoc。http://unfoldingmaps.org/javadoc/de/fhpotsdam/unfolding/marker/AbstractMarker.html#getScreenPosition(de.fhpotsdam.unfolding.UnfoldingMap)

背后的原因是(我认为)标记是通过 map.addMarker 方法添加到地图中,然后与 map.draw() 方法一起渲染,其中经纬度在后台转移到它们的屏幕位置.

因为您试图在 map.draw() 方法之后将国家图标添加到国家标记的顶部,所以您应该尝试将图标的位置与标记的屏幕位置相关联。

    public void draw() {
        background(200);
        map.draw();
        
        for (Marker m: countryMarkers) {
            
            image(countryIcon, (((AbstractMarker) m).getScreenPosition(map).x,
 ((AbstractMarker) m).getScreenPosition(map).y);
        }
    }

如果您将 CountryMarker 类扩展为 SimplePointMarker 类(可在展开的地图库中使用),它还有助于代码看起来更有条理和更干净。

于 2020-06-26T04:15:17.137 回答