3

There is a component called MapPanel, which displays OpenStreetMaps.

I want to mark some places on the displayed map, without changing the underlying map. Those marked places are identified by latitude and longitude.

In order to implement this, I wrote following class, which should convert latitude and longitude to pixel coordinates.

@Override
public Point convert(double aLatitude, double aLongitude, Rectangle bounds) {

    final Point pointInMapUnits = mapPanel.computePosition(new Point.Double(aLatitude, aLongitude));

    Point mapCenter = mapPanel.getCenterPosition();
    Dimension mapSize = mapPanel.getMapSize();

    final Rectangle panelSize = bounds;
    final Point2D.Double panelCenter = new Point.Double(panelSize.x + panelSize.getWidth()/2,
            panelSize.y + panelSize.getHeight()/2);

    final double ourObjectXFromMapCenterInMapUnits = pointInMapUnits.getX() - mapCenter.getX();
    final double ourObjectYFromMapCenterInMapUnits = pointInMapUnits.getY() - mapCenter.getY();

    final double ourObjectXFromMapCenterInPixels = convertToPixelsX(ourObjectXFromMapCenterInMapUnits, panelSize,
            mapSize, mapPanel.getZoom());
    final double ourObjectYFromMapCenterInPixels = convertToPixelsY(ourObjectYFromMapCenterInMapUnits, panelSize,
            mapSize,mapPanel.getZoom());

    final Point result = new Point((int)(panelCenter.getX() + ourObjectXFromMapCenterInPixels),
            (int)(panelCenter.getY() + ourObjectYFromMapCenterInPixels));

    return result;
}

But it doesn't work as following screenshots illustrate.

Step 1: Map is centered at the point in question (132.18381, 43.35514). That place is marked by a red dot.

Image 1

Step 2: Now we scroll the map so that the marked place is located to the left of the position in step 1.

The blue dot shows where the red dot should be.

Image 2

Step 3: Now we scroll the map so that the marked place is at the top.

Again the blue dot shows the expected position of the red dot.

Image 3

Here's a video of the process.

You can find the code here.

How can I change it, in order for the dot to always be displayed at the point (132.18381, 43.35514) ?

1

I added a new method to the MapPanel which does what you want. The method is called getScreenCoordinates. I just moved the MapPanel to github, so you need to get the latest version from

https://github.com/srutz/mappanel

public Point convert(double aLatitude, double aLongitude, Rectangle bounds) {
  return mapPanel.getScreenCoordinates(aLatitude, aLongitude);
}

That does what you want. The code wasn't very clear in the first place. Please note that i fixed a number of issues so its best to get the mappanel fresh. sorry that you loose your setGlasspane stuff, but if you need some kind of repaint-listener maybe we should add a listener for just that. What do you think?

in that case you or i could make it. if you want to, a pull-request is welcome.

greetings, stepan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.