3

I'm trying to put the OSMDroid to work with WMS, but I do not find a way to put the WMS working.

Objective: OSMDroid working with WMS (projection EPSG:4326)

Tentative: I followed this example and include files: WMSMapTileProviderBasic, WMSMapTileDownloader, WMSTileSource, MapTile, and put the following code in my activity:

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);
    }
}

but did not show anything in map, why?

Thank you for your time.

Johnitro
  • 31
  • 6

1 Answers1

0

You need to convert the ESPG:4326 to WebMercantor BB format. I have successfully used this code for doing that:

/*
* 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;
    }

}

Then you can create a tile with a WMSTileProvider source which extends the OnlineTileSourceBase and overrides the getTileURLString by converting it to the X,Y,Zoom to WebMercatorBoundingBox.

 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;
    }

}
IdaM
  • 17
  • 3