如何使用私有提供商使用的Web地图服务(WMS)来使用OSMDroid?

时间:2015-02-10 15:55:57

标签: java android osmdroid wms map-projections

我正在尝试将OSMDroid与WMS配合使用,但我找不到让WMS正常工作的方法。

目标:使用WMS的OSMDroid(投影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);
    }
}

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

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您需要将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源创建一个tile,它扩展了OnlineTileSourceBase并通过将其转换为X,Y,Zoom to 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;
    }

}