gpt4 book ai didi

java - 使用 OpenStreetMaps JMapViewer 移动 map 标记

转载 作者:行者123 更新时间:2023-12-02 07:14:09 25 4
gpt4 key购买 nike

我正在 Swing 中使用 JMapViewer 创建 map 。我在 map 上有几个代表汽车的 MapMarkerDots。我正在尝试更新这些标记的位置,以便它们看起来像是在 map 上行驶,但它工作得不太正常。我有一个“汽车”要遵循的坐标列表,但发生的情况是位置已更新,但标记在完成之前不会重新绘制,这意味着标记是在初始位置和最终位置绘制的,而不是两者之间的每一点。我为此使用的代码如下。知道为什么会发生这种情况吗?

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 听起来像是一个后台线程,因此您需要对 event dispatch thread 进行任何更新(EDT) 与 SwingUtilities.invokeLater。如果有效的话,SwingWorker可能是让您的 Daemon 在工作线程的 process 方法中定期进行 EDT 更新的好方法。

关于java - 使用 OpenStreetMaps JMapViewer 移动 map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15156587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com