JMapViewer ADD tiles for offline view

橙三吉。 提交于 2019-12-11 02:25:50

问题


Hello I'm using this sample in Java to try to load OpenStreetMaps Offline tiles,

for example I have my tiles on C:/OSM/tiles/

but I need to know how to add this information in map (JMapViewer) class to load tiles locally.

Thank you very much for your help this is my source:

//License: GPL. Copyright 2008 by Jan Peter Stotz

import org.openstreetmap.gui.jmapviewer.JMapViewer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * Demonstrates the usage of {@link JMapViewer}
 *
 * @author Jan Peter Stotz
 *
 */
public class Demo extends JFrame {

    public Demo() {
        super("JMapViewer Demo");
        setSize(400, 400);
        final JMapViewer map = new JMapViewer();
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        JPanel panel = new JPanel();
        add(panel, BorderLayout.NORTH);
        final JCheckBox showMapMarker = new JCheckBox("Map markers visible");
        showMapMarker.setSelected(map.getMapMarkersVisible());
        showMapMarker.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                map.setMapMarkerVisible(showMapMarker.isSelected());
            }
        });
        panel.add(showMapMarker);
        final JCheckBox showTileGrid = new JCheckBox("Tile grid visible");
        showTileGrid.setSelected(map.isTileGridVisible());
        showTileGrid.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                map.setTileGridVisible(showTileGrid.isSelected());
            }
        });
        panel.add(showTileGrid);
        final JCheckBox showZoomControls = new JCheckBox("Show zoom controls");
        showZoomControls.setSelected(map.getZoomContolsVisible());
        showZoomControls.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                map.setZoomContolsVisible(showZoomControls.isSelected());
            }
        });
        panel.add(showZoomControls);
        add(map, BorderLayout.CENTER);

        //
//      map.addMapMarker(new MapMarkerDot(49.814284999, 8.642065999));
//      map.addMapMarker(new MapMarkerDot(49.91, 8.24));
//      map.addMapMarker(new MapMarkerDot(49.71, 8.64));
//      map.addMapMarker(new MapMarkerDot(48.71, -1));
//      map.addMapMarker(new MapMarkerDot(49.807, 8.644));

        map.setDisplayPositionByLatLon(-0.223056, -78.5126, 11);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new Demo().setVisible(true);
    }

}

回答1:


A much better alternative that doesn't require modifying JMapViewer and recompiling it is to provide your own TileSource implementation, as shown here.

Grab the OfflineOsmTileSource, and use it like the blog post says.

Simple and elegant. All you need is some osm tiles stored locally, which I assume you already have.




回答2:


As far as I remember JMapViewer is designed to work only using online maps.

Changing that behavior seams to be complicated. May be you can achieve this by implementing your own org.openstreetmap.gui.jmapviewer.TileLoader instance. The implementation only has to be able to create Runnable instances that load a specific tile into the TileCache and inform the registered TileLoaderListener that a tile loading is completed.




回答3:


I compile by the source directly and change

\org\openstreetmap\gui\jmapviewer\tilesources\AbstractOsmTileSource.java

Here is the source code inside JMapViewer.zip/JMapViewer_src.jar extract the Jar file and copy the folder /org in the mail source code folder

http://svn.openstreetmap.org/applications/viewer/jmapviewer/releases/2011-02-19/JMapViewer.zip

and change the next

 public AbstractOsmTileSource(String name, String base_url, String attr_img_url) {
        this.name = name;
     // this.baseUrl = base_url;
        this.baseUrl = "file:///C:/OSM/tiles";
        attrImgUrl = attr_img_url;
    }



回答4:


I have no idea if this approach wasn't supported back when this thread happened, but for caching offline tiles they provide OsmFileCacheTileLoader;

http://josm.openstreetmap.de/doc/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.html

It is super easy to use.

this.mapViewer = new JMapViewer();
OsmFileCacheTileLoader ofctl;
try {
    File cacheDir = new File(System.getProperty("user.home"), "OpenStreetMapTileCache");
    logger.info("Home Directory = " + System.getProperty("user.home") + ", cacheDir=" + cacheDir);
    cacheDir.mkdirs();
    ofctl = new OsmFileCacheTileLoader(mapViewer, cacheDir);
    this.mapViewer.setTileLoader(ofctl);
} catch (IOException ex) {
    Logger.getLogger(MapDisplayPanel.class.getName()).log(Level.SEVERE, "Exception creating OsmFileCacheTileLoader" + ex, ex);
}


来源:https://stackoverflow.com/questions/10657239/jmapviewer-add-tiles-for-offline-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!