【问题标题】:How to save geoposition location in java如何在java中保存地理位置
【发布时间】:2016-06-17 14:56:52
【问题描述】:

当用户点击地图时,我从地图中获取坐标。现在我想保存它并将坐标传递给另一个类。如果有人知道答案,请指导。

谢谢

public class Corndinates extends MapClickListener{
    /**
     * Creates a mouse listener for the jxmapviewer which returns the
     * GeoPosition of the the point where the mouse was clicked.
     *
     * @param viewer the jxmapviewer
     */
    public Corndinates(JXMapViewer viewer) {
        super(viewer);
    }


    @Override
    public void mapClicked(GeoPosition location) {

        GeoPosition  cord = location;
        System.out.println(cord);

    }
}

我想将绳子传递给下面的班级并将位置添加到这一行

//初始化第一个和最后一个位置(程序从鼠标点击这里获取坐标)

    GeoPosition firstPoint = new GeoPosition(50.834722, 12.921389);

但我不知道如何从绳索中将值放入 firstPoint 对象中。

public class MapPanel {

public static void main(String args) {
    System.out.println(args);

    JFrame frame = new JFrame("FrameWork");
    FrameWork frameWork = new FrameWork();
    frame.setContentPane(frameWork.mainPanel);

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

    // Use 8 threads in parallel to load the tiles
    //tileFactory.setThreadPoolSize(8);

    //Initializing first and last position (program to get the coordinate from mouse click here)
    GeoPosition firstPoint = new GeoPosition(50.834722, 12.921389);
    GeoPosition lastPoint = new GeoPosition(50.839167, 12.9275);

    // Create a track from the geo-positions
    List<GeoPosition> track = Arrays.asList(firstPoint,lastPoint);
    RoutePainter routePainter = new RoutePainter(track);

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

    //Set the focus
    frameWork.mapViewer.setZoom(7);
    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));

    // Create waypoints from the geo-positions
    Set<SwingWaypoint> waypoints = new HashSet<SwingWaypoint>(Arrays.asList(
            new SwingWaypoint("Zentrum", firstPoint),
            new SwingWaypoint("TU", lastPoint)));

    // Create a waypoint painter that takes all the waypoints
    WaypointPainter<Waypoint> waypointPainter = new WaypointPainter<Waypoint>();
    waypointPainter.setWaypoints(waypoints);

    // Create a compound painter that uses both the route-painter and the waypoint-painter
    List<org.jxmapviewer.painter.Painter<JXMapViewer>> painters = new ArrayList<org.jxmapviewer.painter.Painter<JXMapViewer>>();
    painters.add(routePainter);
    painters.add(waypointPainter);

    CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
    frameWork.mapViewer.setOverlayPainter(painter);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);


}

}

【问题讨论】:

  • 保存是什么意思?在数据库中?
  • 我的意思是我想在另一个类中使用,所以如何将坐标传递给其他类。或者如何存储在数组中?
  • 因为绳子包含纬度和经度,在我想使用这些坐标的类中接受字符串参数。
  • 如果你的数据库是一个mongoDB很容易存储所有坐标的位置对象,如果不是你可以创建一个类Coordinate,并保存N-records或这种类型

标签: java jxmapviewer


【解决方案1】:

通过传递给另一个类,我假设您希望避免将信息保存到文件中。以下代码会将代码传递给您的新类

@Override
public void mapClicked(GeoPosition location) {

    GeoPosition  cord = location;
    System.out.println(cord);
    NewClass c = new NewClass(cord);

}

public class NewClass {

    GeoPosition location = null;

    public NewClass(GeoPosition coord) { //constructor to hold location in class
        location = coord;
    }

    public doCalc {
        //etc
    }
}

【讨论】:

  • 我已经编辑了我的问题,并将类的代码放在了我想传递“绳索”的地方,事情是当我手动将cordiates放在我的点上时,一切正常,但我想要当用户点击在地图上,那些坐标应该用于“firstPoint”。我不知道该怎么做,我试过但失败了。
猜你喜欢
  • 2019-05-03
  • 2013-08-13
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多