【问题标题】:How to work OSMDroid with Web Map Service (WMS) used by the private provider?如何将 OSMDroid 与私人提供商使用的 Web 地图服务 (WMS) 一起使用?
【发布时间】:2015-04-10 17:51:03
【问题描述】:

我正在尝试让 OSMDroid 与 WMS 一起工作,但我找不到让 WMS 工作的方法。

目标:OSMDroid 使用 WMS(投影 EPSG:4326)

暂定:我遵循example 并包含文件:WMSMapTileProviderBasic、WMSMapTileDownloader、WMSTileSource、MapTile,并将以下代码放入我的活动中:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

        // get the mapview holder
        LinearLayout mapHolder = (LinearLayout) findViewById(R.id.my_osmdroid_mapview);
        
        // create a new WMS provider

        final WMSMapTileProviderBasic tileProvider = new WMSMapTileProviderBasic(getApplicationContext());

        // create the WMS tile source
        final ITileSource tileSource = new WMSTileSource("WMS", null, 1, 20, 256, ".jpg", "http://myserver.com/geoserver/"); 
        tileProvider.setTileSource(tileSource);
        
        // create a new basic map view
        MapView mapView = new MapView(this, 256, new DefaultResourceProxyImpl(this), tileProvider);
        
        // add the layout params to the view so the map fills the screen
        mapView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,      ViewGroup.LayoutParams.MATCH_PARENT));
        
        // add the mapview to display
        mapHolder.addView(mapView);
    }
}

但在地图上没有显示任何内容,为什么?

感谢您的宝贵时间。

【问题讨论】:

    标签: java android osmdroid wms map-projections


    【解决方案1】:

    您需要将 ESPG:4326 转换为 WebMercantor BB 格式。我已成功使用此代码:

    /*
    * Sources http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
    * code based on  https://www.azavea.com/blog/2013/01/14/wms-on-android/
    */
    
    public class WebMercatorBoundingBox {
        double north;
        double south;
        double east;   
        double west;
    
    
        // Web Mercator n/w corner of the map.
         private static final double[] TILE_ORIGIN = {-20037508.34789244,    20037508.34789244};
        //array indexes for that data
        private static final int ORIG_X = 0;
        private static final int ORIG_Y = 1; // "
    
        // Size of square world map in meters, using WebMerc projection.
        private static final double MAP_SIZE = 20037508.34789244 * 2;
    
        protected WebMercatorBoundingBox(final int x, final int y, final int zoom) {
            north = tile2lat(y, zoom);
            south = tile2lat(y + 1, zoom);
            west = tile2lon(x, zoom);
            east = tile2lon(x + 1, zoom);
        }
        double tile2lon(int x, int z) {
            double tileSize = MAP_SIZE / Math.pow(2.0, z);
            return  TILE_ORIGIN[ORIG_X] + x * tileSize;
        }
    
        double tile2lat(int y, int z) {
            double tileSize = MAP_SIZE / Math.pow(2.0, z);
            return TILE_ORIGIN[ORIG_Y] - y * tileSize;
        }
    
        public double getNorth(){
            return this.north;
        }
        public double getSouth(){
            return this.south;
        }
        public double getEast(){
            return this.east;
        }
        public double getWest(){
            return this.west;
        }
    
    }
    

    然后您可以创建一个带有 WMSTileProvider 源的图块,该源扩展了 OnlineTileSourceBase 并通过将其转换为 X、Y、Zoom 到 WebMercatorBoundingBox 来覆盖 getTileURLString。

     public class TileProviderFactory{
    
         public static WMSTileProvider getWmsTileProvider(String version, String url, String layers, String fileFormat) {
            String[] baseUrl = {url};
            final String WMS_FORMAT_STRING =
                url +
                "?service=WMS" +
                "&version=" + version +
                "&request=GetMap" +
                "&LAYERS=" + layers +
                "&bbox=%f,%f,%f,%f" +
                "&width=256" +
                "&height=256" +
                "&srs=EPSG:3857" +
                "&format=" + fileFormat;
    
        WMSTileProvider tileProvider = new WMSTileProvider(baseUrl, 256) {
    
            @Override
            public String getTileURLString(MapTile mapTile) {
                WebMercatorBoundingBox bb = new WebMercatorBoundingBox(mapTile.getX(), mapTile.getY(),     mapTile.getZoomLevel());
                    String s = String.format(
                            Locale.ENGLISH,
                            WMS_FORMAT_STRING,
                            bb.getWest(),
                            bb.getSouth(),
                            bb.getEast(),
                            bb.getNorth());
                   Log.d(TAG,"Fetching map tile: " + s);
                   return s;
               }
            };
            return tileProvider;
        }
    }
    
    
    public abstract class WMSTileProvider extends OnlineTileSourceBase {
    
        // cql filters
        private String cqlString = "";
    
        // Construct with tile size in pixels, normally 256, see parent class.
        public WMSTileProvider(String[] baseurl, int tileSizeInPixels) {
            super("WMS tile source", 0 ,20,tileSizeInPixels,"png",baseurl);
    
        }
    
        protected String getCql() {
            return URLEncoder.encode(cqlString);
        }
    
        public void setCql(String c) {
            cqlString = c;
        }
    
    }
    

    【讨论】:

    • 能否添加调用地图的完整示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2021-09-05
    • 2016-06-08
    • 2012-10-15
    • 2017-02-05
    相关资源
    最近更新 更多