[英]how to get a map position automatically mapsforge android
我正在使用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();
但是這些方法返回空值。 如何自動獲取地圖位置?
例如,您可以添加一個按鈕來計算您的位置
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);
}
}
});
如果要為特定地圖文件設置地圖起始位置,則可以在創建時將其嵌入地圖文件中。 例如,以下滲透命令將創建一個名為mr.map的地圖文件,其中包含特定的邊界框和起始位置:
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
在您的軟件中,通過執行以下操作來設置初始地圖位置:
Layers layers = mMapView.getLayerManager().getLayers();
mMapViewPosition = mMapView.getModel().mapViewPosition;
TileRendererLayer trl = AndroidUtil.createTileRendererLayer(mTileCache,
mMapViewPosition , mapFile, getRenderTheme(), false, true);
layers.add(trl);
mMapViewPosition = mMapView.getModel().mapViewPosition;
mMapViewPosition.setCenter(trl.getMapDataStore().startPosition());
您可以使用以下代碼來保存和恢復頭寸:
將位置保存在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.