如何获得一个地图位置自动地图为ge android



我正在使用mapsfage 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:utilsmapsforge-0.5.2my-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);
}

相关内容

  • 没有找到相关文章