1

I am new to Java programming. I want to get the coordinates of map in Java when the user click on map. How I can get these coordinates in 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);
    }
}
scai
  • 20,297
  • 4
  • 56
  • 72
Suleman Jamil
  • 43
  • 1
  • 7
  • Display the map in a `JLabel` that is itself put in a layout/constraint that does not stretch the component. Add a `MouseListener` to the label. On mouse click, get the co-ordinates from the event object. Translate those numbers appropriate for the map scale and location. .. For any more help than that basic run-down, post your best attempt at implementing it, in a [mcve]. One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). One of those images is a map. – Andrew Thompson Jun 05 '16 at 12:50
  • BTW - you've added the tag for OpenStreetMap. Are you using those maps specifically? – Andrew Thompson Jun 05 '16 at 12:52
  • @AndrewThompson yes i am using open street map. – Suleman Jamil Jun 05 '16 at 13:07
  • Where is the MCVE? – Andrew Thompson Jun 05 '16 at 13:18
  • could you guide me about the code given below – Suleman Jamil Jun 05 '16 at 14:12
  • 1
    BTW - to ensure that the code *is an MCVE,* paste it into a new project, add the open street API and try to compile it. That would reveal that the code is ***not*** an MCVE since custom classes like `SwingWaypoint` are not defined.. – Andrew Thompson Jun 05 '16 at 14:53

0 Answers0