我正在使用 mapsforge 0.5.0 库开发一个应用程序。显示地图的代码是标准的:
private MapView mapView;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidGraphicFactory.createInstance(this.getApplication());
this.mapView = new MapView(this);
this.mapView.setClickable(true);
this.mapView.getMapScaleBar().setVisible(true);
this.mapView.setBuiltInZoomControls(true);
this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);
// create a tile cache of suitable size
this.tileCache =AndroidUtil.createTileCache(this,
"mapcache", mapView.getModel().displayModel.getTileSize(), 1f,
this.mapView.getModel().frameBufferModel.getOverdrawFactor());
}
@Override
protected void onStart() {
super.onStart();
this.mapView.getModel().mapViewPosition.setCenter(new LatLong(55.73417, 37.676045));
this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);
// tile renderer layer using internal render theme
this.tileRendererLayer = new TileRendererLayer(tileCache,
this.mapView.getModel().mapViewPosition, false, true,
AndroidGraphicFactory.INSTANCE);
tileRendererLayer.setMapFile(getMapFile());
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
// only once a layer is associated with a mapView the rendering starts
this.mapView.getLayerManager().getLayers().add(tileRendererLayer);
}
@Override
protected void onStop() {
super.onStop();
this.mapView.getLayerManager().getLayers().remove(this.tileRendererLayer);
this.tileRendererLayer.onDestroy();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.tileCache.destroy();
this.mapView.getModel().mapViewPosition.destroy();
this.mapView.destroy();
AndroidResourceBitmap.clearResourceBitmaps();
}
private File getMapFile() {
SharedPreferences mfPref = getSharedPreferences("PREF_MAP_FILE",MODE_PRIVATE);
String mapFilePath = mfPref.getString("mapFileKey", "0");
File file = null;
if (mapFilePath.indexOf(".map") != -1) { file = new File(mapFilePath); }
return file;
}
代替
this.mapView.getModel().mapViewPosition.setCenter(new LatLong(55.73417, 37.676045));
我想自动居中地图(以防我可以从不同的文件加载不同的地图)
我试过了
this.tileRendererLayer.getPosition();
和
this.mapView.getModel().mapViewPosition.getMapLimit().getCenterPoint();
但是这些方法返回空值。如何自动获取地图位置?