在 OSMDroid 中显示来自 Geoserver 的地图
Display map from Geoserver in OSMDroid
我已经在 Geoserver 中缓存了图块,我需要在我的 android 应用程序中通过 OSMDroid 显示地图。我已经在我的带有 OpenLayers 的 Web 应用程序中尝试过它并且工作正常,如下所示:
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://localhost:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/{z}/{x}/{-y}.png'
})
})
所以现在我需要在 Android 中显示它。我为自定义图块源创建了 class:
public class GeoserverTileSource extends OnlineTileSourceBase {
public static String[] TILE_URL = {"http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png"};
public GeoserverTileSource(String aName, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding, String[] aBaseUrl) {
super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
}
@Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + "/" + aTile.getZoomLevel() + "/" + aTile.getX() + "/" + aTile.getY();
}
}
这是我的 MainActivity class:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
//important! set your user agent to prevent getting banned from the osm servers
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_main);
MapView map = (MapView) findViewById(R.id.map);
GeoserverTileSource source = new GeoserverTileSource("geoserver", 10, 18, 256, ".png", GeoserverTileSource.TILE_URL);
map.setTileSource(source);
//map.setTileSource(TileSourceFactory.MAPNIK);
}
public void onResume(){
super.onResume();
Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
}
}
地理服务器日志打印如下:
2017-03-04 14:29:20,451 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/642/281
2017-03-04 14:29:20,460 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/639/281
2017-03-04 14:29:20,460 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/640/280
2017-03-04 14:29:20,472 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/637/281
2017-03-04 14:29:20,473 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/638/281
2017-03-04 14:29:20,480 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/639/280
2017-03-04 14:29:20,480 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/641/279
地理服务器打开地理服务器调试日志后:
http://pastebin.com/wTw5ASDC
起初我试过 Mapnik tile source 并且它有效......但我没有:/你能告诉我我做错了什么吗?谢谢
编辑:
我已将我的函数 getTileURLString 编辑为此,但仍然无法正常工作:
@Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + "/" + aTile.getZoomLevel() + "/" + aTile.getX() + "/" + aTile.getY() + ".png";
}
所以我在 brwser 的日志文件中尝试 url,我得到了这个:
当我尝试 url 最后加上“.png”时,我得到了这个:
android 工作室终端打印此:
W/OsmDroid: Problem downloading MapTile: /10/651/444 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/649/444 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/648/445 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/650/445 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/649/445 HTTP response:
osmdroid 调用 url 是否可能使用了错误的参数?因为正如您在图片上看到的那样,结构名称略有不同,并且它们包含下划线。
GeoServer 说它不知道如何处理这样的请求:
http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/11/9/3
这是因为您没有指定图片格式,所以 URL 这样的应该可以:
http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/11/9/3.png
我已经在 Geoserver 中缓存了图块,我需要在我的 android 应用程序中通过 OSMDroid 显示地图。我已经在我的带有 OpenLayers 的 Web 应用程序中尝试过它并且工作正常,如下所示:
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://localhost:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/{z}/{x}/{-y}.png'
})
})
所以现在我需要在 Android 中显示它。我为自定义图块源创建了 class:
public class GeoserverTileSource extends OnlineTileSourceBase {
public static String[] TILE_URL = {"http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png"};
public GeoserverTileSource(String aName, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding, String[] aBaseUrl) {
super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
}
@Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + "/" + aTile.getZoomLevel() + "/" + aTile.getX() + "/" + aTile.getY();
}
}
这是我的 MainActivity class:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
//important! set your user agent to prevent getting banned from the osm servers
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_main);
MapView map = (MapView) findViewById(R.id.map);
GeoserverTileSource source = new GeoserverTileSource("geoserver", 10, 18, 256, ".png", GeoserverTileSource.TILE_URL);
map.setTileSource(source);
//map.setTileSource(TileSourceFactory.MAPNIK);
}
public void onResume(){
super.onResume();
Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
}
}
地理服务器日志打印如下:
2017-03-04 14:29:20,451 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/642/281
2017-03-04 14:29:20,460 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/639/281
2017-03-04 14:29:20,460 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/640/280
2017-03-04 14:29:20,472 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/637/281
2017-03-04 14:29:20,473 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/638/281
2017-03-04 14:29:20,480 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/639/280
2017-03-04 14:29:20,480 ERROR [geowebcache.GeoWebCacheDispatcher] - null http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/18/641/279
地理服务器打开地理服务器调试日志后: http://pastebin.com/wTw5ASDC
起初我试过 Mapnik tile source 并且它有效......但我没有:/你能告诉我我做错了什么吗?谢谢
编辑: 我已将我的函数 getTileURLString 编辑为此,但仍然无法正常工作:
@Override
public String getTileURLString(MapTile aTile) {
return TILE_URL[0] + "/" + aTile.getZoomLevel() + "/" + aTile.getX() + "/" + aTile.getY() + ".png";
}
所以我在 brwser 的日志文件中尝试 url,我得到了这个:
当我尝试 url 最后加上“.png”时,我得到了这个:
android 工作室终端打印此:
W/OsmDroid: Problem downloading MapTile: /10/651/444 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/649/444 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/648/445 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/650/445 HTTP response:
W/OsmDroid: Problem downloading MapTile: /10/649/445 HTTP response:
osmdroid 调用 url 是否可能使用了错误的参数?因为正如您在图片上看到的那样,结构名称略有不同,并且它们包含下划线。
GeoServer 说它不知道如何处理这样的请求:
http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/11/9/3
这是因为您没有指定图片格式,所以 URL 这样的应该可以:
http://10.0.2.2:8080/geoserver/gwc/service/tms/1.0.0/Bachelor%3Av_slovensko_group@EPSG%3A3857@png/11/9/3.png