[英]Moving map markers with OpenStreetMaps JMapViewer
我正在使用Swing中的JMapViewer創建一個地圖。 我在地圖上有幾個代表汽車的MapMarkerDots。 我正在嘗試更新這些標記的位置,以便它們看起來像是在地圖上行駛但是它不能正常工作。 我有一個“汽車”要遵循的坐標列表,但會發生的是位置更新但標記不會重繪,直到完成為止意味着標記在初始位置和最終位置繪制而不是在兩者之間的每一點。 我正在使用的代碼如下。 知道為什么會這樣嗎?
public void drawRoute(String id){
MapMarkerDot mmd;
String evMarkerObject; // ID and Marker position
String[] items, locations;
double lat, lon;
for(int i = 0; i < route.length-1; i+=2){ // Iterate through the route
List markers = zmap.getMapMarkerList(); // Get the markers that are currently on the map
for(int j = 0; j < Daemon.evMarkers.size(); j++){ // Go through the list of recorded marker IDs and locations
evMarkerObject = Arrays.toString(Daemon.evMarkers.get(j)); // Get marker id and location
items = evMarkerObject.split(", "); // Split the ID and location
if(items[0].substring(1).equals(id)){ // If an ID match is found
locations = items[1].split(" "); // Split location values by " "
lat = Double.parseDouble(locations[2]); // Get latitude of marker
lon = Double.parseDouble(locations[3]); // Get longitude of marker
for(int k = 0; k < markers.size(); k++){ // Go through list of markers currently on map
mmd = (MapMarkerDot) markers.get(k); // Get each marker in turn
if((mmd.getLat() == lat) && (mmd.getLon() == lon)){ // Check if recorded position matches marker position
zmap.removeMapMarker(mmd); // Remove marker from the map
break; // Break from loop (appropriate marker found)
}
}
Daemon.evMarkers.remove(j); // Remove record of marker ID and position
zaddMarker(Color.BLUE, route[i], route[i+1], 'e', items[0].substring(1)); // Add marker at new position
//zmap.repaint();
}
}
}
調用該函數(基於@Catalina的答案):
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){
@Override
protected Void doInBackground() throws Exception {
drawRoute(markerID);
return null;
}
};
worker.execute();
這是在鼠標單擊事件上調用的。
Daemon
聽起來像一個后台線程,因此您需要使用SwingUtilities.invokeLater
對事件調度線程 (EDT)進行任何更新。 如果SwingWorker
, SwingWorker
可能是讓您的Daemon
在工作process
方法中定期進行EDT更新的好方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.