Ticket #14796: JMapViewer-3.patch

File JMapViewer-3.patch, 6.4 KB (added by chris.tipper@…, 8 years ago)

revert OsmTileSource.java and add template for thunderforest transport map to demonstrate use of API key (cycle maps is no different in the end)

  • src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

     
    4444 */
    4545public class JMapViewer extends JPanel implements TileLoaderListener {
    4646
     47    private static final long serialVersionUID = 1L;
     48
    4749    /** whether debug mode is enabled or not */
    4850    public static boolean debug;
    4951
     
    502504            return (int) marker.getRadius();
    503505        else if (p != null) {
    504506            Integer radius = getLatOffset(marker.getLat(), marker.getLon(), marker.getRadius(), false);
    505             radius = radius == null ? null : p.y - radius.intValue();
     507            radius = radius == null ? null : p.y - radius;
    506508            return radius;
    507509        } else
    508510            return null;
  • src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

     
    99import java.util.HashMap;
    1010import java.util.Map;
    1111import java.util.Map.Entry;
    12 import java.util.concurrent.Executors;
     12import java.util.concurrent.LinkedBlockingQueue;
    1313import java.util.concurrent.ThreadPoolExecutor;
     14import java.util.concurrent.TimeUnit;
    1415
    1516import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    1617import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
     
    2223 * @author Jan Peter Stotz
    2324 */
    2425public class OsmTileLoader implements TileLoader {
    25     private static final ThreadPoolExecutor jobDispatcher = (ThreadPoolExecutor) Executors.newFixedThreadPool(3);
     26   
     27    private static final ThreadPoolExecutor jobDispatcher = new ThreadPoolExecutor(8, 24, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(2_000));
    2628
    2729    private final class OsmTileJob implements TileJob {
    2830        private final Tile tile;
     
    178180
    179181    @Override
    180182    public void cancelOutstandingTasks() {
    181         jobDispatcher.getQueue().clear();
     183        for (Runnable item : jobDispatcher.getQueue()) {
     184            jobDispatcher.remove(item);
     185        }
    182186    }
    183187
    184188    /**
  • src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

     
    263263            }
    264264        }
    265265        try {
    266             return attributions.get(0, TimeUnit.MILLISECONDS);
     266            return attributions.get(1500, TimeUnit.MILLISECONDS);
    267267        } catch (TimeoutException ex) {
    268268            System.err.println("Bing: attribution data is not yet loaded.");
    269269        } catch (ExecutionException ex) {
  • src/org/openstreetmap/gui/jmapviewer/tilesources/OsmTileSource.java

     
    11// License: GPL. For details, see Readme.txt file.
    22package org.openstreetmap.gui.jmapviewer.tilesources;
    33
     4import java.io.IOException;
     5import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
     6
    47/**
    58 * OSM Tile source.
    69 */
     
    6265            return 18;
    6366        }
    6467    }
     68
     69    /**
     70     * The "Transport Map" OSM tile source.
     71     */
     72    public static class TransportMap extends AbstractOsmTileSource {
     73
     74        private static final String API_KEY = "API_KEY_HERE";
     75
     76        private static final String PATTERN = "https://%s.tile.thunderforest.com/transport";
     77
     78        private static final String[] SERVER = { "a", "b", "c" };
     79
     80        private int serverNum;
     81
     82        /**
     83         * Constructs a new {@code TransportMap} tile source.
     84         */
     85        public TransportMap() {
     86            super("OSM Transport Map", PATTERN, "osmtransportmap");
     87        }
     88
     89        @Override
     90        public String getBaseUrl() {
     91            String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});
     92            serverNum = (serverNum + 1) % SERVER.length;
     93            return url;
     94        }
     95
     96        @Override
     97        public int getMaxZoom() {
     98            return 18;
     99        }
     100
     101        @Override
     102        public String getTileUrl(int zoom, int tilex, int tiley) throws IOException {
     103            return this.getBaseUrl() + getTilePath(zoom, tilex, tiley); // + "?apikey=" + API_KEY;
     104        }
     105
     106        @Override
     107        public String getAttributionText(int zoom, ICoordinate topLeft, ICoordinate botRight) {
     108            return "Maps © Thunderforest, Data © OpenStreetMap contributors";
     109        }
     110
     111        @Override
     112        public String getAttributionLinkURL() {
     113            return "http://www.thunderforest.com/";
     114        }
     115    }
     116
    65117}
  • src/org/openstreetmap/gui/jmapviewer/tilesources/ScanexTileSource.java

     
    116116    @Override
    117117    public Point latLonToXY(double lat, double lon, int zoom) {
    118118        return new Point(
    119                 (int) osmMercator.lonToX(lon, zoom),
    120                 (int) latToTileY(lat, zoom)
     119                (int) Math.round(osmMercator.lonToX(lon, zoom)),
     120                (int) Math.round(latToTileY(lat, zoom))
    121121                );
    122122    }
    123123
  • src/org/openstreetmap/gui/jmapviewer/tilesources/TMSTileSource.java

     
    5050    @Override
    5151    public Point latLonToXY(double lat, double lon, int zoom) {
    5252        return new Point(
    53                 (int) osmMercator.lonToX(lon, zoom),
    54                 (int) osmMercator.latToY(lat, zoom)
     53                (int) Math.round(osmMercator.lonToX(lon, zoom)),
     54                (int) Math.round(osmMercator.latToY(lat, zoom))
    5555                );
    5656    }
    5757