如何在java中保存地理位置

如何在java中保存地理位置,java,jxmapviewer,Java,Jxmapviewer,当用户单击地图时,我从地图上获取坐标。现在我想保存这个,并将坐标传递给另一个类。如果有人知道答案,请引导 谢谢 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.

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

谢谢

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

    }
}
我想把绳子传给下面的班级,并把位置加到这条线上

//初始化第一个和最后一个位置(通过鼠标单击此处获取坐标的程序)

但我不知道如何把价值从电线第一点的对象

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);
公共类映射面板{
公共静态void main(字符串参数){
系统输出打印项次(args);
JFrame=新JFrame(“框架”);
框架=新框架();
frame.setContentPane(frameWork.mainPanel);
//为OpenStreetMap创建TileFactoryInfo
TileFactoryInfo=新的OSMTileFactoryInfo();
DefaultTileFactory tileFactory=新的DefaultTileFactory(信息);
frameWork.mapViewer.setTileFactory(tileFactory);
//使用8条平行螺纹加载瓷砖
//tileFactory.setThreadPoolSize(8);
//初始化第一个和最后一个位置(通过鼠标单击此处获取坐标的程序)
地理位置第一点=新地理位置(50.834722,12.921389);
地理位置lastPoint=新地理位置(50.839167,12.9275);
//从地理位置创建轨迹
List track=Arrays.asList(firstPoint,lastPoint);
RoutePainter RoutePainter=新的RoutePainter(轨道);
//设置默认位置
地理位置chemnitz=新地理位置(50.833333,12.916667);
//设定焦点
frameWork.mappeviewer.setZoom(7);
frameWork.mapViewer.setAddressLocation(chemnitz);
//添加交互
MouseInputListener mia=新的PanMouseInputListener(frameWork.mapViewer);
frameWork.mapViewer.addMouseListener(mia);
frameWork.mapViewer.addMouseMotionListener(mia);
//frameWork.mapViewer.addMouseListener(新的CenterMapListener(frameWork.mapViewer)); frameWork.mapViewer.addMouseWheelListener(新的ZoomMouseWheelListenerCenter(frameWork.mapViewer)); //frameWork.mapViewer.addKeyListener(新的PanKeyListener(frameWork.mapViewer))

//从地理位置创建航路点
设置航路点=新哈希集(Arrays.asList(
新SwingWaypoint(“顶点”,firstPoint),
新SwingWaypoint(“TU”,lastPoint));
//创建一个获取所有航路点的航路点绘制程序
WaypointPainter WaypointPainter=新的WaypointPainter();
航路点。设定航路点(航路点);
//创建同时使用路线绘制器和航路点绘制器的复合绘制器
列表绘制者=新的ArrayList();
油漆工。添加(routePainter);
画师。添加(航路点画师);
复合画家=新的复合画家(画家);
frameWork.mapViewer.setOverlayPainter(painter);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

}

通过传递到另一个类,我认为您希望避免将信息保存到文件中。下面的代码将把代码传递到新类中

@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
    }
}

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

@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
    }
}

“保存”是什么意思?在DB中?我的意思是我想在另一个类中使用,所以如何将坐标传递给另一个类。或者如何存储在数组中?因为跳线包含纬度和经度,在我要使用这些坐标的类中接受字符串参数。如果您的DB是mongoDB,则很容易存储具有所有坐标的location对象,如果不是,则可以创建一个类坐标,并保存N条记录或此类型保存它意味着什么?在DB中?我的意思是我想在另一个类中使用,所以如何将坐标传递给另一个类。或者如何存储在数组中?因为跳线包含纬度和经度,在我要使用这些坐标的类中接受字符串参数。如果您的DB是mongoDB,则很容易存储具有所有坐标的location对象,如果不是,则可以创建类坐标,保存N-记录或这种类型我已经编辑了我的问题,并把类的代码放在我想要通过“线”的地方。当我手动将坐标放在我的点上时,一切正常,但我希望当用户单击地图时,这些坐标应该用于“第一点”。我不知道怎么做,我试过了,但是失败了。我已经编辑了我的问题,并且把我想通过的类的代码放在了“cordinate”的位置。事情是当我手动将cordinate放在我的点上时,一切正常,但我想当用户点击地图时,这些cordinate应该用于“firstPoint”。我不知道怎么做,我试过了,但失败了。
@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
    }
}