简体   繁体   English

使用OpenStreetMaps JMapViewer移动地图标记

[英]Moving map markers with OpenStreetMaps JMapViewer

I am creating a map using JMapViewer in Swing. 我正在使用Swing中的JMapViewer创建一个地图。 I have several MapMarkerDots on the map which represent cars. 我在地图上有几个代表汽车的MapMarkerDots。 I'm trying to update the positions of these markers so that they appear to be driving around the map however it's not quite working properly. 我正在尝试更新这些标记的位置,以便它们看起来像是在地图上行驶但是它不能正常工作。 I have a list of coordinates that the "car" is to follow but what happens is that the positions are updated but the markers are not redrawn until this is finished meaning that the marker is drawn at the initial location and at the final location rather that at every point between the two. 我有一个“汽车”要遵循的坐标列表,但会发生的是位置更新但标记不会重绘,直到完成为止意味着标记在初始位置和最终位置绘制而不是在两者之间的每一点。 The code I'm using for this is below. 我正在使用的代码如下。 Any idea as to why this may be happening? 知道为什么会这样吗?

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();
            }
        }
    }

Calling the function (based on answer from @Catalina): 调用该函数(基于@Catalina的答案):

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

                                @Override
                                protected Void doInBackground() throws Exception {
                                    drawRoute(markerID);
                                    return null;
                                }
                            };

                            worker.execute();

This is called on a mouse click event. 这是在鼠标单击事件上调用的。

Daemon sounds like a background thread, so you need to do any updates on the event dispatch thread (EDT) with SwingUtilities.invokeLater . Daemon听起来像一个后台线程,因此您需要使用SwingUtilities.invokeLater事件调度线程 (EDT)进行任何更新。 If that works, SwingWorker might be a good way to make your Daemon do regular EDT updates in the worker's process method. 如果SwingWorkerSwingWorker可能是让您的Daemon在工作process方法中定期进行EDT更新的好方法。

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

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