2016-06-17 4 views
1

Я получаю координаты с карты, когда пользователь нажимает на карту. Теперь я хочу сохранить это и передать координаты другому классу. Если кто-нибудь знает ответ, пожалуйста, посоветуйте.Как сохранить местоположение геопозиции в java

Благодаря

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 (новый CenterMapListener (frameWork.mapViewer)); frameWork.mapViewer.addMouseWheelListener (новый ZoomMouseWheelListenerCenter (frameWork.mapViewer)); // frameWork.mapViewer.addKeyListener (новый 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); 


} 

}

+0

Что вы имеете в виду? в БД? – cralfaro

+0

Я имею ввиду, что хочу использовать в другом классе, чтобы передать координату другому классу. или как хранить в массиве? –

+0

Поскольку шнур содержит широту и долготу, в классе, где я хочу использовать эти координаты, принимаем аргументы String. –

ответ

0

По пропуском в другой класс, я полагаю, вы хотите, чтобы избежать сохранения информации в файл. Следующий код передаст код в ваш новый класс

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

Я отредактировал свой вопрос, а также поместил код класса где я хочу передать «шнур», когда дело в том, что я вкладываю в мою сторону вещи, когда все работает нормально, но я хочу, когда пользователь нажимает на карту, тогда эти сердечники должны использоваться для «firstPoint». Я не знаю, как это сделать, я пытался, но не смог. –