我正在使用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();
但是theese方法返回null值。如何自动获取地图位置?
答案 0 :(得分:0)
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPSTracker(getApplicationContext());
//check if the GPS is active
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
posiz = new LatLong(latitude, longitude);
mapView.getModel().mapViewPosition.setCenter(posiz);
updateMarker(posiz);
}
}
});
答案 1 :(得分:0)
如果要为特定地图文件设置地图起始位置,可以在创建地图文件时将其嵌入地图文件中。例如,以下渗透命令创建一个名为mr.map的映射文件,其中包含特定的边界框和起始位置:
String fileName = location + "/" + "client_" + clientId + ".xml";
File clientFile = new File(fileName);
Document doc = parseXML(clientFile);
// now how to use doc object?
在您的软件中,通过执行以下操作设置初始地图位置:
osmosis --rb file=merged --mapfile-writer file=mr.map bbox=-34.4103,114.9184,-33.4864,115.4265 map-start-position=-33.61,115.13 map-start-zoom=11 tag-conf-file=c:\utils\mapsforge-0.5.2\my-tag-mapping.xml
答案 2 :(得分:0)
您可以使用此代码保存和恢复位置:
在onDestry()的首选项中保存位置:
@Override
protected void onDestroy() {
MapViewPosition mvp = mapView.getModel().mapViewPosition;
LatLong latLon = mvp.getCenter();
SharedPreferences.Editor ed = preferences.edit();
ed.putString("mapViewCnterLat",String.valueOf(latLon.getLatitude()));
ed.putString("mapViewCnterLon",String.valueOf(latLon.getLongitude()));
ed.putInt("mapViewCnterZoom",mvp.getZoomLevel());
ed.commit();
super.onDestroy();
}
并在onCreate()中恢复它们:
@Override
protected void onCreate(Bundle savedInstanceState) {
String lat = preferences.getString("mapViewCnterLat", String.valueOf(MAP_DEFAULT_LATITUDE));
String lon = preferences.getString("mapViewCnterLon", String.valueOf(MAP_DEFAULT_LONGITUDE));
int zoomLevel = preferences.getInt("mapViewCnterZoom",11);
this.mapView.setCenter(new LatLong(Double.valueOf(lat), Double.valueOf(lon)));
//this.mapView.setCenter(new LatLong(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
this.mapView.setZoomLevel((byte) zoomLevel);
}