gpt4 book ai didi

java - 如何将 OSMDroid 与私有(private)提供商使用的网络 map 服务 (WMS) 一起使用?

转载 作者:太空狗 更新时间:2023-10-29 15:26:33 26 4
gpt4 key购买 nike

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

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

暂定:我关注了这个example并包含文件:WMSMapTileProviderBasic、WMSMapTileDownloader、WMSTileSource、MapTile,并将以下代码放入我的 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);
}
}

但是在 map 上没有显示任何东西,为什么?

感谢您的宝贵时间。

最佳答案

您需要将 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 源创建一个图 block ,它扩展了 OnlineTileSourceBase 并通过将其转换为 X、Y、缩放到 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;
}

}

关于java - 如何将 OSMDroid 与私有(private)提供商使用的网络 map 服务 (WMS) 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28436050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com