【问题标题】:How to add the marker(waypoints) on the map (OSM) whenever user click on map?每当用户单击地图时,如何在地图(OSM)上添加标记(航点)?
【发布时间】:2016-08-08 12:50:14
【问题描述】:

每当用户点击地图时,我想在地图上添加航点。每当用户单击时,我都会在地图上找到路点,但问题是前一个路点消失并且未显示在地图上,仅显示由于当前单击而绘制的 wapoint。以下是航点的代码。

public class MapPanel {

 public static void acc(GeoPosition loc){
  MapPanel.drawNew(loc);
}

 public  static void drawNew(GeoPosition location ){

    GeoPosition fp = new GeoPosition(location.getLatitude(),location.getLongitude());
    List<GeoPosition> track =  Arrays.asList(fp);

//       Create waypoints from the geo-positions
    Set<Waypoint> waypoints = new HashSet<Waypoint>(Arrays.asList(
            new DefaultWaypoint(fp)));
//       Create a waypoint painter that takes all the waypoints
    waypointPainter.setWaypoints(waypoints);
//       Create a compound painter that uses both the route-painter and the waypoint-painter
    List<org.jxmapviewer.painter.Painter<org.jxmapviewer.JXMapViewer>> painters = new ArrayList<org.jxmapviewer.painter.Painter<org.jxmapviewer.JXMapViewer>>();
    painters.add(waypointPainter);
    CompoundPainter<org.jxmapviewer.JXMapViewer> painter = new CompoundPainter<org.jxmapviewer.JXMapViewer>(painters);
    frameWork.mapViewer.setOverlayPainter(painter);

}
public static void main (String args) {
    frame.setContentPane(frameWork.mainPanel);

    // Create a TileFactoryInfo for OpenStreetMap
    TileFactoryInfo info = new OSMTileFactoryInfo();
    DefaultTileFactory tileFactory = new DefaultTileFactory(info);
    frameWork.mapViewer.setTileFactory(tileFactory);

    // Set the Default Location
   GeoPosition chemnitz = new GeoPosition(50.833333, 12.916667);

    //Set the focus
    frameWork.mapViewer.setZoom(1);
    frameWork.mapViewer.setAddressLocation(chemnitz);

    // Add interactions
    MouseInputListener mia = new PanMouseInputListener(frameWork.mapViewer);

    frameWork.mapViewer.addMouseListener(mia);
    frameWork.mapViewer.addMouseMotionListener(mia);
    frameWork.mapViewer.addMouseListener(new CenterMapListener(frameWork.mapViewer));
    frameWork.mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(frameWork.mapViewer));
    frameWork.mapViewer.addKeyListener(new   PanKeyListener(frameWork.mapViewer));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.pack();
    frame.setSize(900, 600);
    frame.setVisible(true);


}
}

【问题讨论】:

    标签: java swing openstreetmap jxmapviewer jxmapkit


    【解决方案1】:

    如果没有完整的源代码,我只能假设问题可能出在 1-您用于存储航点的模型:确保将新单击的点添加到模型中,不会被最后一个覆盖(检查所选航点的大小) 或者 2-您正在使用的视图为每个添加事件完全重新绘制;导致所有在最后绘制之前的元素丢失。

    【讨论】:

    • 我已经编辑了代码,您现在可以重新检查一下吗
    • 抱歉 Jamil 但代码不完整尝试进行我告诉你的检查,确保单击的位置都存储在模型中(要检查这一点,你可以分析单击映射通知的对象是什么)
    • 我可以理解,但我被困在这一点上 drawNew 方法在地图上绘制航点,但我不明白为什么它没有在地图上显示以前的航点。我必须在 drawNew 方法中进行什么样的修改,以便它可以显示以前的航点。
    • 要么您只存储最后一个位置并绘制它(检查存储位置的变量,您可能每次都覆盖此变量内容而不是保留它)
    • 当我第一次在地图上单击时,它会在该点上显示标记,但是当我在地图上单击第二次时,它会在第二次单击点上显示标记,而第一个航路点标记也会消失进一步点击只显示一个航点标记
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2016-10-23
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多