2016-06-17 5 views
1

사용자가지도를 클릭하면지도에서 좌표를 가져옵니다. 이제 이것을 저장하고 좌표를 다른 클래스에 전달하려고합니다. 답변을 아는 분이면 누구나 안내하십시오.자바에서 geoposition 위치를 저장하는 방법

감사

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

저장하는 것은 무엇을 의미합니까? DB에? – 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"에 사용되어야한다. 나는 이것을 해보지 만 실패했다. –

관련 문제