1

我正在使用 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();

但是这些方法返回空值。如何自动获取地图位置?

4

3 回答 3

0

如果要为特定地图文件设置地图起始位置,可以在创建地图文件时将该位置嵌入到地图文件中。例如,以下 osmosis 命令创建一个名为 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());
于 2015-09-25T03:56:59.003 回答
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);

                } 
        }
    });
于 2015-09-06T18:03:55.613 回答
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);

}
于 2016-07-31T19:50:32.683 回答