繁体   English   中英

使用OpenStreetMaps JMapViewer移动地图标记

[英]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)进行任何更新。 如果SwingWorkerSwingWorker可能是让您的Daemon在工作process方法中定期进行EDT更新的好方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM