1

So i am new to JMapViewer (and StackOverFlow). What I am currently trying to do is to develop a real time graphical representation of a network, where the background is a map of the area. So far it's been going well, however one issue is bugging me. When I create a MapMarkerCircle, and set the name, I want to create a new line, to show information below it.

I've tried "\n", but this won't work. I've tried enclosing it in html format and using <br> to break the line, but again, it just prints the entire thing as if it was a string.

If anyone has come across this issue before, I would really appreciate any help.

Here is a small bit of code where the problem is occuring. Note that "RateCircle" extends "MapMarkerCircle".

Coordinate dataPoint= new Coordinate((pmpLinks[i].getRecieverSite().getLat()+pmpLinks[i].getTransmitter().getLat())/2, (pmpLinks[i].getRecieverSite().getLon()+pmpLinks[i].getTransmitter().getLon())/2);                   
String rateStringName="<html>"+inRateAsString+"<br>kb/s</html>";

pmpCanopyRatePoints[i]=new RateCircle(allPMPrateLayer[i],rateStringName, dataPoint);                    
map().addMapMarker(pmpCanopyRatePoints[i]);
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85

2 Answers2

1

MapMarkerCircle::paint calls MapMarkerCircle::paintText, which calls Graphics::drawString, which accords no special meaning to control characters or markup. Starting from this example, the implementation of paintText() below draws a second line beneath the first.

I've updated the example to suggest a way to associate the marker's name and value. It uses a Map<String, Integer>, passing a Map.Entry<String, Integer> as a parameter to the RateCircle constructor.

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerCircle;
import org.openstreetmap.gui.jmapviewer.Style;

/**
 * @see https://stackoverflow.com/a/38265252/230513
 * @see https://stackoverflow.com/a/33857113/230513
 */
public class RateCircleTest {

    private void display() {
        JFrame f = new JFrame("London");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMapViewer map = new JMapViewer() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        Coordinate london = new Coordinate(51.5072, -0.1275);
        map.setDisplayPosition(london, 16);
        Map<String, Integer> rates = new HashMap<>();
        rates.put("London", 42);
        for (Map.Entry<String, Integer> entry : rates.entrySet()) {
            map.addMapMarker(new RateCircle(entry, london));
        }
        f.add(map);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class RateCircle extends MapMarkerCircle {

        private static final int R = 12;
        private Map.Entry<String, Integer> entry;

        public RateCircle(Map.Entry<String, Integer> entry, Coordinate coord) {
            super(null, "", coord, R, STYLE.FIXED, getDefaultStyle());
            this.entry = entry;
            Style style = getStyle();
            style.setBackColor(Color.cyan);
            style.setColor(Color.red);
        }

        @Override
        public void paintText(Graphics g, Point position) {
            super.paintText(g, position);
            g.drawString(entry.getKey(), position.x + R + 2, position.y + R);
            g.drawString(entry.getValue() + " kb/s", position.x + R + 2,
                position.y + R + g.getFontMetrics().getHeight());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new RateCircleTest()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

You can use System.lineSeparator():

String rateStringName = inRateAsString + System.lineSeparator() + "kb/s";
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85