1

我是 Java 编程新手。当用户单击地图时,我想在 Java 中获取地图的坐标。如何在 Java 中获取这些坐标?

package com.example.java;

import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.OSMTileFactoryInfo;
import org.jxmapviewer.input.CenterMapListener;
import org.jxmapviewer.input.PanKeyListener;
import org.jxmapviewer.input.PanMouseInputListener;
import org.jxmapviewer.input.ZoomMouseWheelListenerCenter;
import org.jxmapviewer.painter.CompoundPainter;
import org.jxmapviewer.viewer.*;
import sample7_swingwaypoints.SwingWaypoint;

import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.util.*;

/**
 * Created by Suleman on 6/5/2016.
 */
public class MapPanel {

    public static void main(String[] args) {
        JXMapViewer mapViewer = new JXMapViewer();

        // Create a TileFactoryInfo for OpenStreetMap
        TileFactoryInfo info = new OSMTileFactoryInfo();
        DefaultTileFactory tileFactory = new DefaultTileFactory(info);
        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
        mapViewer.setZoom(7);
        mapViewer.setAddressLocation(chemnitz);

        // Add interactions
        MouseInputListener mia = new PanMouseInputListener(mapViewer);
        mapViewer.addMouseListener(mia);
        mapViewer.addMouseMotionListener(mia);
        mapViewer.addMouseListener(new CenterMapListener(mapViewer));
        mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
        mapViewer.addKeyListener(new PanKeyListener(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);
        mapViewer.setOverlayPainter(painter);

        // Display the viewer in a JFrame
        JFrame frame = new JFrame("FRAMEWORK");
        frame.getContentPane().add(mapViewer);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
4

0 回答 0